【问题标题】:@PostConstruct method is not called in Spring@PostConstruct 方法在 Spring 中不被调用
【发布时间】:2010-08-08 13:00:05
【问题描述】:

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 方法没有被调用。

【问题讨论】:

    标签: spring


    【解决方案1】:

    您需要&lt;context:annotation-config/&gt;(或&lt;context:component-scan/&gt;)来启用@PostConstruct 处理。

    【讨论】:

    • 是的,它现在正在工作..是的..我记得前段时间我了解到..我们需要这些元素来识别注释..
    猜你喜欢
    • 2017-02-15
    • 2016-09-29
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 2014-02-04
    • 2016-04-06
    • 2019-07-01
    相关资源
    最近更新 更多