Results 1 to 3 of 3

Thread: Injection of Properties fails

  1. #1
    Join Date
    Dec 2011
    Posts
    15

    Default Injection of Properties fails

    Hi folks,

    i have the following problem:

    I define a bean in my applicationContext:

    Code:
    <bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="locations">
                <list>
                    <value>file:/etc/acodyn/collector.properties</value>
                </list>
            </property>
        </bean>
    In a junit test the loading of the applicationContext works well until another bean will be initialized through the context:

    Code:
    <bean id="collectorBean" name="collectorBean" class="de.test.collectorBean" depends-on="applicationProperties" />
    In my understanding if I use the following code, the collectorBean should wait until the applicationProperties are initilized and ready for injection.

    Code:
    public class CallCollectorProviderObserverBean implements ProviderObserver{
        
        final Logger logger = LoggerFactory.getLogger(CallCollectorProviderObserverBean.class);
        Condition conditionInService = new Condition();
        
        @Autowired
        private Properties applicationProperties;
    
        public collectorBean() {
            
            initialize();
            
        }
        
        private void initialize(){
            
            String host = applicationProperties.getProperty("host");
            String user = applicationProperties.getProperty("user");
    
        }
    }
    But when I start loading the applicationContext, I get a NullPointerException because applicationProperties is NULL.

    Any suggestions what can be wrong?

    Cheers and thanks in advance
    Peter

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default


    But when I start loading the applicationContext, I get a NullPointerException because applicationProperties is NULL.
    Which isn't that strange it is what I expected to happen...

    You expect a dependency to be injected BEFORE an instance of your class exists.. Not sure how you have that in mind but I can assure you that is NEVER going to happen. Remove the call to initialize from your constructor and simple put @PostContruct on your initialize method.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Dec 2011
    Posts
    15

    Default

    Aaahhh... here we go...

    thanks mate for the hint.

    I thought with adding the depends-on attribute the container will wait for processing the constructor until the applicationProperties are completely initialized.

    Well everyday is a good day to learn something new.

    Again many thanks...

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •