简体   繁体   中英

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? Does it get called during the PostConstruct lifecycle phase when all the bean definitions are loaded and bean instances are created? If so, does the method get called again during the spring scan for @Bean annotations?

Update:

I have some properties present in SampleContext which I am using to initialize SampleClass bean from the sampleMethod method. If I don't use postConstruct and use only @Bean, it looks like sampleMethod() is getting called before SampleContext has initialized.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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