相关疑难解决方法(0)

在Spring中没有调用@PostConstruct方法

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");

    }
Run Code Online (Sandbox Code Playgroud)

和配置文件如下:

<bean id="SampleBean" class="com.springexample.SampleBean">
    <property name="beanOne" ref="beanOneOne"></property>
    <property name="beanTwo" ref="beanTwoOne"></property>
</bean>
Run Code Online (Sandbox Code Playgroud)

我没有在beans标记上设置default-init-method属性.

任何人都可以告诉为什么@PostConstruct方法不会被调用.

spring

23
推荐指数
1
解决办法
2万
查看次数

Spring:当我们还有自定义init()或@PostConstuct时,为什么需要在InitializingBean的afterPropertiesSet

为什么afterPropertiesSetInitializingBean需要的时候,我们也有定制init() @Bean(initMethod = "init")@PostConstuct?我可以用一个做什么而不做另一个做什么?我何时应该使用一个而不是另一个.在所有属性自动装配后触发所有回调.

java spring javabeans

2
推荐指数
1
解决办法
3054
查看次数

标签 统计

spring ×2

java ×1

javabeans ×1