首页 > 解决方案 > @Configuration 类中 @Bean 和 @PostConstruct 的 Spring 调用顺序

问题描述

我有一个像这样设置的配置类:

@Configuration
public class MyConfigClass {
   private String field1;

   @PostConstruct
   public void init() {
       field1 = < system property >
   }

   @Bean
   MyService myService() {
       // initialize MyService and use field1
   }
}

我想知道 init() 方法是否总是在 myService() 方法之前运行,因为 myService() 方法依赖于 init 运行来设置某些系统属性的值。

我知道我可以在 myService() 中初始化 field1,但我对在上面显示的确切场景中会发生什么感兴趣。

标签: javaspringspring-bootspring-annotations

解决方案


推荐阅读