Spring不调用@PostConstruct方法 [英] @PostConstruct method is not called in Spring

查看:72
本文介绍了Spring不调用@PostConstruct方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SampleBean:

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 属性.

And I don't have default-init-method attribute set on the beans tag.

任何机构都可以说出为什么@PostConstruct方法没有被调用的原因.

Can any body tell why the @PostConstruct method does not get called.

推荐答案

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

You need <context:annotation-config/> (or <context:component-scan/>) to enable @PostConstruct handling.

这篇关于Spring不调用@PostConstruct方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆