Issue
I have a java,spring and not spring boot command line program with maven , which when i build a jar with all dependencies using maven-assembly-plugin , it includes application.properties, what i need to know is how to read the external application.properties and not the jar one.
I am reading the property file as:
@PropertySource(value = { "classpath:application.properties" })
If I print the classpath, the classpath does only include the jar and not the current directory.
Solution
Here is what you can do:
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource(value = "file:/etc/config/my-custom-config.properties", ignoreResourceNotFound = true)
})
This ignoreResourceNotFound
is available since spring 4.3 and is pretty self-explanatory.
You can also opt for "programmatic" approach. In pure spring (not spring boot as you've mentioned in the question):
@Configuration
public class CommonConfig {
...
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
ppc.setLocations(new FileSystemResource("/etc/config/my-custom-config.properties"),
new ClassPathResource("config/application.properties"),
return ppc;
}
...
}
FileSystemResource
is for accessing resources available externally in the file system
ClassPathResource
is for accessing resources in the classpath
Answered By - Mark Bramnik
Answer Checked By - Cary Denson (JavaFixing Admin)