Announcement Announcement Module
Collapse
No announcement yet.
Property File getting read, but value not getting set. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Property File getting read, but value not getting set.

    Hi, I'm hoping someone can help with this. The goal is to load different properties depending on different spring.profiles.active values. I have a mix of java and xml config. The xml file is read and processed. The properties file that i want read based on the profile that is being read (according to the logs). However using the @Value in my @Configuration class is not getting set. I don't seem to have anyway of getting to the properties.

    Context:

    Code:
        
    
        <context:component-scan base-package="com.companyname"/>
    
        <beans profile="localDev">
            <context:property-placeholder location="classpath:localDev.properties"/>
        </beans>
    
        <beans profile="test">
            <util:properties id="properties-test" location="classpath:/test.properties"/>
        </beans>
    
        <beans profile="production">
            <util:properties id="properties-production" location="classpath:/production.properties"/>
        </beans>

    Init:

    Code:
            System.setProperty("spring.profiles.active", profile);
            GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
            ctx.getEnvironment().setActiveProfiles(profile);
            ctx.load("classpath:applicationContext.xml");
            ctx.refresh();
            Map<String,Object> props = ctx.getEnvironment().getSystemProperties();
            for(String key : props.keySet()){  //not sure it supposed to set properties in system properties or not.
                System.out.println(key); //desperate attempt to find them.
            }

    Config:

    Code:
    package com.companyname
    @Configuration
    public class AppConfig {
    
        @Autowired
        private Environment environment;
    
        @Value("${ccctu.testValue}")
        String testValue;
         ...

    Properties localDev.properties:

    Code:
    ccctu.testValue=ugh

    Logs:

    Code:
    Apr 17, 2013 12:06:49 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
    Apr 17, 2013 12:06:49 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing [email protected]c51355: startup date [Wed Apr 17 12:06:49 EDT 2013]; root of context hierarchy
    Apr 17, 2013 12:06:49 PM org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
    INFO: Loading properties file from class path resource [localDev.properties]
    Apr 17, 2013 12:06:49 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1c56c60: defining beans [appConfig,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
    Test:
    Code:
           App app = new App();
            app.init("localDev");
            AppConfig appConfig = new AppConfig();
    
            assertNotNull(appConfig.getTestValue()); //fail
            assertNotNull(appConfig.getEnvironment()); //fail

    Questions:
    1: Can anyone see what I'm doing wrong?
    2: If I configure a property-placeholder should the properties get added to system properties?
    3: Is my initialization of GenericXmlApplicationContext correct? I have tried many implementations with the same result.
    4: Slightly off topic is the @Autowired Environment environment a convention that Spring follows? Does that get set automatically? I thought maybe it would and i put it in there to see i can get anything set at all. It did not.

    References:
    1: http://static.springsource.org/sprin...tml#beans-java
    2: http://static.springsource.org/sprin...lderconfigurer

    System: Windows7 32, Java 6, Spring 3.2.2
    Last edited by jgreenaw; Apr 19th, 2013, 08:41 AM.
Working...
X