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>
而且我没有在 beans 标记上设置 default-init-method 属性。
任何人都可以说出为什么@PostConstruct 方法没有被调用。
最佳答案
您需要<context:annotation-config/>
(或 <context:component-scan/>
)启用 @PostConstruct
处理。
关于spring - @PostConstruct 方法在 Spring 中不被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3434377/