Announcement Announcement Module
Collapse
No announcement yet.
Injection of Properties fails Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

    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.

    Comment


    • #3
      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

      Comment

      Working...
      X