spring - @PostConstruct 方法在 Spring 中不被调用

标签 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 属性。

任何人都可以说出为什么@PostConstruct 方法没有被调用。

最佳答案

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

关于spring - @PostConstruct 方法在 Spring 中不被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3434377/

相关文章:

spring - 具有 Spring Data Rest 功能的自定义 Spring MVC HTTP 补丁请求

java - 如何在 spring mvc&thymeleaf 中进行验证和异常

从 Controller 调用时出现 java.lang.NullPointerException

java - Spring 安全 : Accessing secured resources using cookie returned at login

java - 在 Hibernate 中多次获取父类

java - 如何在manyToMany字段中实现 View ?

java - 如何从Spring Boot连接在线MongoDB数据库?

java - 从查询中获取较旧的时间戳

java - ClassCastException $Proxy 无法转换为使用 aop

spring - 为什么我们需要在 spring-boot-starter-jersey 项目中为 Jersey 资源提供 @Component spring 注解?