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"); }
和配置文件是这样的:
<bean id="SampleBean" class="com.springexample.SampleBean"> <property name="beanOne" ref="beanOneOne"></property> <property name="beanTwo" ref="beanTwoOne"></property> </bean>
而且我没有在bean标记上设置default-init-method属性。
任何机构都可以说出为什么@PostConstruct方法没有被调用的原因。
你需要<context:annotation-config/>(或<context:component-scan/>)启用@PostConstruct处理功能。
<context:annotation-config/>
<context:component-scan/>
@PostConstruct