Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override Spring 3.1 @PropertySource to set IgnoreResourceNotFound & IgnoreUnresolvablePlaceholders

Having this class to override an already set @PropertySource in another Config class.

@Configuration
@PropertySource({ "classpath:${env.placeholder}/app.properties" })
public class PropertyOverrideConfig {

}

But whenever the file or placeholder is missing, its failing the context loading. I need to set the following flags to that annotation loaded property, so that it will skip, if its not able to find the property.

        setIgnoreResourceNotFound(true);
        setIgnoreUnresolvablePlaceholders(true);

Question1: What would be the appropriate way to set these flags for the @PropertySource?

Updates: Tried adding a @Bean to the same class without that annotation referring this page, it ain't picking the property file either. I don't have an xml configuration.

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
     final PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();

     Resource[] resources = new ClassPathResource[ ] { 
                  new ClassPathResource( "classpath:${env.placeholder}/app.properties" ) };

     pspc.setLocations( resources );
     pspc.setIgnoreResourceNotFound(true);
     pspc.setIgnoreUnresolvablePlaceholders(true);
     return pspc;
}

Question2: I am sure I'm missing something, but couldn't figure out what it is, any help would be great.

like image 392
raksja Avatar asked Jan 08 '13 21:01

raksja


2 Answers

I think I finally figured out the answer for the Question1:

How to set Ignore flags for properties added through @PropertySource?

Its not there in Spring still, its been proposed as an Minor Improvement. Hopefully will be able to see an additional attribute added to the annotation soon in future release.

SPR-8371

Still not sure about the way to accomplish the scenario mentioned and no answer for Question2.

like image 184
raksja Avatar answered Nov 09 '22 23:11

raksja


Not really a solution, but a workaround, if you have to stick to Spring 3: use a dummy default, which exists, i.e. in your case:

@PropertySource({ "classpath:${env.placeholder:dummy}/app.properties" })
like image 24
Yannick Block Avatar answered Nov 10 '22 00:11

Yannick Block