Instantiate beans in order in Spring?

前端 未结 3 1644
礼貌的吻别
礼貌的吻别 2020-12-16 21:19

Is it possible to set order of instantiation in Spring?

I don\'t want to use @DependsOn and I don\'t want to use Ordered interface. I just

3条回答
  •  眼角桃花
    2020-12-16 21:44

    If you want to make sure that a specific bean is created before another bean you can use the @DependsOn annotation.

    @Configuration
    public class Configuration {
    
       @Bean 
       public Foo foo() {
       ...
       }
    
       @Bean
       @DependsOn("foo")
       public Bar bar() {
       ...
       }
    }
    

    Keep in mind that this does not set the order, it only guarantees that the bean "foo" is created before "bar". JavaDoc for @DependsOn

提交回复
热议问题