Spring Boot如何在加载bean时优先选择我
2023-03-14 10:35:38 来源:易采站长站 作者:
目录引言一、适用场景二、三种实现方式1.@Configuration注解+@DependsOn注解2.@Component注解+@DependsOn注解3.实现Priority...
目录
引言一、适用场景
二、三种实现方式
1. @Configuration 注解 + @DependsOn 注解
2. @Component 注解 + @DependsOn 注解
3. 实现 PriorityOrdered 接口
三、参考资料
引言
Spring Boot 是当前业界最受欢迎和广泛编程使用的 Java Web 应用开发框架之一。在 Spring Boot 应用中,bean
是通过自动配置进行装载的,因为其按照约定顺序位置,Spring Boot
希望尽可能提供正确的自动配置,在应用运行时重写或自定义扩展。这样,bean
的优先级可以在应用程序的所有层次结构中管理。如果我们需要在应用程序启动时优先加载某些 bean,那么本文就是为大家详细介绍如何实现。
一、适用场景
如果我们需要在应用程序的所有层次结构中对特定 bean 的启动顺序进行管理。例如,需要在应用程序启动的时候初始化某个bean。如果我们公共库中的 bean 被其他开发者服务用到,但是他们需要在部分场景下自定义 bean,则我们需要在这些自定义的 bean前面先加载公共库中的 bean。
二、三种实现方式
在 Spring Boot 应用程序中,我们可以采取以下三种方式实现自己的 bean 优先加载:
1. @Configuration 注解 + @DependsOn 注解
@Configuration 注解在 Spring Boot 应用程序中声明 bean 并允许我们指定 bean 的优先级。然后,我们可以使用 @DependsOn 注解明确地告诉 Spring 容器这些 bean 应该在应用程序的哪一阶段被加载。
使用方法如下:
(1) 声明 @Configuration 注解以及使用 @DependsOn 注解并且确保引用的 bean 已经存在(可以是其他的 bean 或配置类)。
@Configuration
@DependsOn("myOrderBean")
public class MyOrderedBeanConfig {
// 配置类内普通Bean
@Bean
public MyBean myBean() {
return new MyBean();
}
}
(2) 将 @Configuration 注解引入到 Spring Boot 应用程序中,以便在应用程序启动时执行。
@SpringBootApplication
@Import(MyOrderedBeanConfig.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2. @Component 注解 + @DependsOn 注解
@Component 注解是最快速的声明 bean 的方法之一,并允许我们指定 bean 的名称。如果我们希望已有的 bean 在应用程序启动时首先被加载,那么我们可以使用 @DependsOn 注解来实现。当指定多个 bean 时,可以使用逗号来分隔。
使用方法如下:
(1) 在使用 @Component 注解的类中,使用 @DependsOn 注解来明确指定 bean 的加载顺序。
@Component("myBean")
@DependsOn({"bean1", "bean2"})
public class MyBean {
// ...
}
(2) 将 @Component 注解引入到 Spring Boot 应用程序中,以便在应用程序启动时执行。
@SpringBootApplication
@ComponentScan(basePackages = "com.example.demo")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3. 实现 PriorityOrdered 接口
实现 PriorityOrdered 接口并实现 getOrder() 方法可以使我们控制 bean 的加载顺序。最后,我们可以使用 BeanPostProcessor 接口来确保这些 bean 第一次生成时被加javascript载。
使用方法如下:
(1) 实现 PriorityOrdered 接口,并使用 getOrder() 方法返回一个正整数以指定 bean 的优先级。该数值越小,优先级越高。
public class MyBean implements InitializingBean, PriorityOrdered {
public void afterPropertiesSet() {
// ...
}
public int getOrder() {
return 0; // 这个值将确保此 bean 被最先加载
}
}
(2) 提供 BeanPostProcessor 实例,并用 @Order 注解指定顺序。在 Spring 容器的生命周期中,此实例将在所有常规 bean 之前执行。
@Component
@Order(value = 1)
public class MyBeanPostProcessor implements BeanPostProcessor {
// ...
}
(3) 将 @ComponentScan 注解引入到 Spring Boot 应用程序中,以便在应用程序启动时执行。
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.demo"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
注意事项
在应用程序中使用上述方式之一都能够帮助您管理 bean 的优先级,但这并不意味着其优先级不会被其他 bean 覆盖。如果想覆盖先前声明的 bean,可以使用相应技术栈的高优先级 bean 来覆盖先前声明的 bean。
可以在所有 bean 上使用 @Order 注解或实现 Ordered 接口来实现 bean 的排序。
使用多个技术栈时,可以混合使用这些技术栈来达到目的。
三、参考资料
Spring Boot 官方文档: Controlling Startup Behaviour of Spring BootApplication
Spring 官方文档: Prioritized Bean Applicatons
Medium: Order execution with Spring Boot
Baeldung: Bean Order in Spring
到此这篇关于Spring Boot如何在加载bean时优先选择我?的文章就介绍到这了,更多相关springboot加载bean优先选择我内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!













闽公网安备 35020302000061号