Announcement Announcement Module
Collapse
No announcement yet.
[Spring 3.1.0.M2] @PropertySource flexibility Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Spring 3.1.0.M2] @PropertySource flexibility

    I was playing with the new Spring 3.1.0.M2 @PropertySource annotation.

    In fact I was trying to migrate

    Code:
    <context:property-placeholder location="classpath*:META-INF/config.properties,classpath*:META-INF/config-${com.google.appengine.runtime.environment}.properties" />
    Note that this code load 2 property files. 1 default, and 1 environment/profile specific derived from the system properties using ${com.google.appengine.runtime.environment}.
    I already tried this with the @PropertySource({"classpath:/META-INF/config.properties", "classpath:/META-INF/config-${com.google.appengine.runtime.environment}.proper ties"}) annotation without any luck.

    Next to that I was think how the container will react if I add multiple @ProperySource annotations to various @Configuration classes. If I would program this I would use the MutablePropertySources which supports this using addFirst(), addBefore() etc. methods. The @ProperySource only allows setting the location or locations.

    E.g. I have

    Code:
    @Configuration
    @PropertySource("classpath:/META-INF/config.properties")
    public class PropertiesConfig {
    }
    
    @Configuration
    @Profile("Development")
    @PropertySource("classpath:/META-INF/config-Development.properties")
    public class DevelopmentPropertiesConfig {
    }
    
    @Configuration
    @Profile("Production")
    @PropertySource("classpath:/META-INF/config-Production.properties")
    public class ProductionPropertiesConfig {
    }
    The config.properties file will contain all my properties which will be overridden by the config-Development or config-Production.properties (dependent on the runtime profile).
    But off course the config-Development or config-Production.properties should take precedence over/override the config.properties.

  • #2
    PS

    I'm using below code since 3.1.0.M1 which works:

    Code:
    public class ContextInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> {
    
        private Logger logger = LoggerFactory.getLogger(this.getClass());
        
        private String runtimeEnvironment;
        
        @Override
        public void initialize(ConfigurableWebApplicationContext ctx) {
            this.runtimeEnvironment = SystemProperty.environment.value().value();
            logger.debug("Initializing [" + this.runtimeEnvironment + "] environment");
            ConfigurableEnvironment environment = ctx.getEnvironment();
            environment.setActiveProfiles(this.runtimeEnvironment);
            environment.getPropertySources().addFirst(new PropertiesPropertySource("properties", applicationProperties()));
        }
        
        private Properties applicationProperties() {
            PropertiesFactoryBean factoryBean = new PropertiesFactoryBean();
            Resource[] locations = {
                    new ClassPathResource("META-INF/config.properties"), 
                    new ClassPathResource("META-INF/config-" + this.runtimeEnvironment + ".properties") };
            factoryBean.setLocations(locations);
            try {
                factoryBean.afterPropertiesSet();
                return factoryBean.getObject();
            }
            catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }
    }
    But - obvious - I would prefer to use @PropertySource annotation.

    Comment


    • #3
      As commented at https://jira.springsource.org/browse/SPR-8442:

      @PropertySource resource locations now support resolution of ${...} placeholders.

      See updated Javadoc at https://build.springsource.org/brows...rtySource.html

      Comment

      Working...
      X