小编典典

Spring中不调用@PostConstruct方法

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>

而且我没有在bean标记上设置default-init-method属性。

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


阅读 2375

收藏
2020-04-21

共1个答案

小编典典

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

2020-04-21