@PostConstruct 方法在 Spring 中不被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3434377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@PostConstruct method is not called in Spring
提问by javanoob
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-methodattribute set on the beanstag.
而且我没有在beans标签上设置default-init-method属性。
Can any body tell why the @PostConstruct method does not get called.
任何人都可以告诉为什么没有调用@PostConstruct 方法。
回答by axtavt
You need <context:annotation-config/>(or <context:component-scan/>) to enable @PostConstructhandling.
您需要<context:annotation-config/>(或<context:component-scan/>)来启用@PostConstruct处理。

