23

SampleBean:

package com.springexample;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class SampleBean {

    private BeanTypeOne beanOne;

    private BeanTypeTwo beanTwo;

    public void init() {

        System.out.println("This is from the init() method");
    }

    @PostConstruct
    public void initAnnotation() {

        System.out.println("This is from the initAnnotation() method");

    }

and config file like this :

<bean id="SampleBean" class="com.springexample.SampleBean">
    <property name="beanOne" ref="beanOneOne"></property>
    <property name="beanTwo" ref="beanTwoOne"></property>
</bean>

And I don't have default-init-method attribute set on the beans tag.

Can any body tell why the @PostConstruct method does not get called.

javanoob
  • 6,070
  • 16
  • 65
  • 88

1 Answers1

44

You need <context:annotation-config/> (or <context:component-scan/>) to enable @PostConstruct handling.

RonK
  • 9,472
  • 8
  • 51
  • 87
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • yes it is working now.. Ya..I remember some time back i learned that..we need to have those elements to identify annotations.. – javanoob Aug 08 '10 at 13:48