Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional @PropertySource location

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?

like image 855
Gerhard Schlager Avatar asked Jul 01 '13 09:07

Gerhard Schlager


People also ask

What is spring config location?

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.

How do you change file location in application properties spring boot?

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.

What is classpath in PropertySource?

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 {}

What is @PropertySource in spring boot?

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.


2 Answers

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)
})
like image 176
matsev Avatar answered Sep 21 '22 03:09

matsev


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) {}
    }

}
like image 44
Koraktor Avatar answered Sep 22 '22 03:09

Koraktor