@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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:06:12  来源:igfitidea点击:

@PostConstruct method is not called in Spring

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处理。