Home  >  Article  >  Java  >  How does SpringBoot select me first when loading beans?

How does SpringBoot select me first when loading beans?

王林
王林forward
2023-05-15 21:13:041261browse

    1. Applicable scenarios

    • If we need to manage the startup sequence of specific beans in all hierarchies of the application . For example, you need to initialize a bean when the application starts.

    • If the beans in our public library are used by other developer services, but they need to customize beans in some scenarios, we need to load them in front of these customized beans. beans in public libraries.

    Two and three implementation methods

    In Spring Boot applications, we can adopt the following three methods to achieve priority loading of our own beans:

    1. @Configuration annotation @DependsOn annotation

    @Configuration annotation declares beans in Spring Boot applications and allows us to specify the priority of the bean. We can then use the @DependsOn annotation to explicitly tell the Spring container at which stage of the application these beans should be loaded.

    The usage method is as follows:

    (1) Declare the @Configuration annotation and use the @DependsOn annotation and ensure that the referenced bean already exists (can be other beans or configuration classes ).

    @Configuration 
    @DependsOn("myOrderBean") 
    public class MyOrderedBeanConfig {
       // 配置类内普通Bean
       @Bean
       public MyBean myBean() {
          return new MyBean();
       }
    }

    (2) Introduce the @Configuration annotation into the Spring Boot application so that it can be executed when the application starts.

    @SpringBootApplication
    @Import(MyOrderedBeanConfig.class)
    public class DemoApplication {
       public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
       }
    }

    2. @Component annotation @DependsOn annotation

    @Component Annotation is one of the fastest ways to declare a bean and allows us to specify the name of the bean. If we want existing beans to be loaded first when the application starts, we can use the @DependsOn annotation to achieve this. When specifying multiple beans, commas can be used to separate them.

    The usage method is as follows:

    (1) In the class annotated with @Component, use the @DependsOn annotation to clearly specify the loading of the bean order.

    @Component("myBean") 
    @DependsOn({"bean1", "bean2"}) 
    public class MyBean {
       // ...
    }

    (2) Introduce the @Component annotation into the Spring Boot application to execute when the application starts.

    @SpringBootApplication 
    @ComponentScan(basePackages = "com.example.demo") 
    public class DemoApplication {
       public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
       }
    }

    3. Implement the PriorityOrdered interface

    Implement the PriorityOrdered interface and implement the getOrder() method to allow us to control the loading order of beans. Finally, we can use the BeanPostProcessor interface to ensure that these beans are loaded the first time they are generated.

    The usage method is as follows:

    (1) Implement the PriorityOrdered interface, and use the getOrder() method to return a positive integer to specify the priority of the bean class. The smaller the value, the higher the priority.

    public class MyBean implements InitializingBean, PriorityOrdered {
       public void afterPropertiesSet() {
           // ...
       }
       public int getOrder() {
          return 0; // 这个值将确保此 bean 被最先加载
       }
    }

    (2) Provide a BeanPostProcessor instance and specify the order with the @Order annotation. This instance will execute before all regular beans in the Spring container's life cycle.

    @Component 
    @Order(value = 1) 
    public class MyBeanPostProcessor implements BeanPostProcessor {
       // ...
    }

    (3) Introduce the @ComponentScan annotation into the Spring Boot application so that it can be executed when the application starts.

    @SpringBootApplication 
    @ComponentScan(basePackages = {"com.example.demo"}) 
    public class DemoApplication {
       public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
       }
    }

    Notes

    • Using one of the above methods in an application can help you manage bean priorities, but this does not This means that its priority will not be overridden by other beans.

    • If you want to overwrite a previously declared bean, you can use a high-priority bean of the corresponding technology stack to overwrite the previously declared bean.

    • You can use the @Order annotation on all beans or implement the Ordered interface to implement bean sorting.

    • When using multiple technology stacks, these technology stacks can be mixed to achieve the goal.

    The above is the detailed content of How does SpringBoot select me first when loading beans?. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete