I'm using Spring 3.2 in a web application and I'd like to have a .properties
file within the classpath which contains default values. The user should be able to use JNDI to define a location where another .properties
is stored which overrides the default values.
The following works as long as the user has set the configLocation
as JNDI property.
@Configuration
@PropertySource({ "classpath:default.properties", "file:${java:comp/env/configLocation}/override.properties" })
public class AppConfig
{
}
However, the external overrides should be optional and so should the JNDI property.
Currently I get an exception (java.io.FileNotFoundException: comp\env\configLocation\app.properties (The system cannot find the path specified)
when the JNDI property is missing.
How can I define optional .properties
that are used only when the JNDI property (configLocation
) is set? Is this even possible with @PropertySource
or is there another solution?
If spring. config. location contains directories (as opposed to files) they should end in / (and will be appended with the names generated from spring.config.name before being loaded). The default search path classpath:,classpath:/config,file:,file:config/ is always used, irrespective of the value of spring.
properties in default location. Spring Boot loads the application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory.
When We define property files using “classpath” as shown in above examples, it searches that file at project classpath and resolve all values. To define multiple properties file @Configuration. @PropertySource(value={"classpath:default.properties","classpath:config.properties"}) public class RestAPIURLConfig {}
Spring @PropertySource annotation is used to provide properties file to Spring Environment. This annotation is used with @Configuration classes. Spring PropertySource annotation is repeatable, means you can have multiple PropertySource on a Configuration class.
As of Spring 4, issue SPR-8371 has been solved. Consequently, the @PropertySource
annotation has a new attribute called ignoreResourceNotFound
that has been added for exactly this purpose. Additionally, there is also the new @PropertySources annotation which allows implementations like:
@PropertySources({
@PropertySource("classpath:default.properties"),
@PropertySource(value = "file:/path_to_file/optional_override.properties", ignoreResourceNotFound = true)
})
If you're not yet on Spring 4 (see matsev's solution), here's a more verbose, but roughly equivalent solution:
@Configuration
@PropertySource("classpath:default.properties")
public class AppConfig {
@Autowired
public void addOptionalProperties(StandardEnvironment environment) {
try {
String localPropertiesPath = environment.resolvePlaceholders("file:${java:comp/env/configLocation}/override.properties");
ResourcePropertySource localPropertySource = new ResourcePropertySource(localPropertiesPath);
environment.getPropertySources().addLast(localPropertySource);
} catch (IOException ignored) {}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With