在Spring中没有调用@PostConstruct方法

时间:2010-08-08 13:00:05

标签: spring

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 属性。< / p>

任何正文都可以告诉为什么@PostConstruct方法不会被调用。

1 个答案:

答案 0 :(得分:43)

您需要<context:annotation-config/>(或<context:component-scan/>)才能启用@PostConstruct处理。