简体   繁体   English

如果同时使用@Bean和@PostConstruct注释java方法的春季行为

[英]Spring behaviour if a java method is annotated with both @Bean and @PostConstruct

I have the following class 我有以下课程

@Configuration
public class SampleContext {

    @Bean
    @PostConstruct
    public SampleClass sampleMethod() {

    }
}

How does this method behave in a spring container? 此方法在弹簧容器中的行为如何? In which phase of the spring lifecycle does the sampleMethod() get called? 在春季生命周期的哪个阶段调用sampleMethod() Does it get called during the PostConstruct lifecycle phase when all the bean definitions are loaded and bean instances are created? 当所有的bean定义都被加载并创建了bean实例时,是否在PostConstruct生命周期阶段调用它? If so, does the method get called again during the spring scan for @Bean annotations? 如果是这样,在春季扫描@Bean注释期间是否再次调用该方法?

Update: 更新:

I have some properties present in SampleContext which I am using to initialize SampleClass bean from the sampleMethod method. 我在SampleContext中具有一些属性,这些属性用于从sampleMethod方法初始化SampleClass bean。 If I don't use postConstruct and use only @Bean, it looks like sampleMethod() is getting called before SampleContext has initialized. 如果我不使用postConstruct而是仅使用@Bean,则似乎在初始化SampleContext之前调用了sampleMethod()。

大概在实例化bean时将调用该方法一次,然后在后构造阶段再次调用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM