Springboot中的@Order如何使用
在spring-boot 2.6.2下测试,@Order并不会影响bean的装载顺序,声明了@Component的类,无论是构造方法、@PostConstruct注解声明的方法,还是实现的InitializingBean接口中的afterPropertiesSet()方法,如果beanClass位于同样的目录层级,这些方法的调用只会受到className的顺序影响:
@Component
@Slf4j
@Order(2)
public class Bean1 implements InitializingBean {
public Bean1() {
log.info("construct bean1");
}
@Override
public void afterPropertiesSet() throws Exception {
log.info("initialled bean1");
}
@PostConstruct
public void post() {
log.info("post bean1");
}
}
@Component
@Slf4j
@Order(1)
public class Bean2 implements InitializingBean {
public Bean2() {
log.info("construct bean2");
}
@Override
public void afterPropertiesSet() throws Exception {
log.info("initialled bean2");
}
@PostConstruct
public void post() {
log.info("post bean2");
}
}
/* 结果打印顺序:
construct bean1
post bean1
initialled bean1
construct bean2
post bean2
initialled bean2
*/观察@Order的注解说明,第一句写着: @Order defines the sort order for an annotated component. 提到这个注解只是对component排序,那么哪里会收到这个排序数值的影响呢?
这里先改造一下代码:
public interface IBean {
void work();
}
@Component
@Slf4j
@Order(2)
public class Bean1 implements InitializingBean,CommandLineRunner,IBean {
public Bean1() {
log.info("construct bean1");
}
@Override
public void afterPropertiesSet() throws Exception {
log.info("initialled bean1");
}
@PostConstruct
public void post() {
log.info("post bean1");
}
@Override
public void run(String... args) throws Exception {
log.info("running bean1");
}
@Override
public void work() {
log.info("bean1 is working");
}
}
@Component
@Slf4j
@Order(1)
public class Bean2 implements InitializingBean, CommandLineRunner, IBean {
public Bean2() {
log.info("construct bean2");
}
@Override
public void afterPropertiesSet() throws Exception {
log.info("initialled bean2");
}
@PostConstruct
public void post() {
log.info("post bean2");
}
@Override
public void run(String... args) throws Exception {
log.info("running bean2");
}
@Override
public void work() {
log.info("bean2 is working");
}
}
@Service
@RequiredArgsConstructor
public class TestService {
private final List<IBean> beans;
public void test(){
beans.forEach(IBean::work);
}
}启动之后执行TestService的test方法,得到如下顺序的日志:
construct bean1
post bean1
initialled bean1
construct bean2
post bean2
initialled bean2
running bean2
running bean1
bean2 is working
bean1 is working
作一下说明,@Order会影响依赖注入的顺序,如果存在同样类型的多个bean,且依赖声明使用了List<BeanInterface>,会将所有bean实例按照Order声明的顺序放入一个ArrayList中注入,如果用的是Collection或者Set则无效,因为类型本身无序。
而CommandLineRunner声明的run方法,会在bean被IOC容器装配完成之后被调用,方法注释简单明了的一句Callback used to run the bean可以理解为bean实例真正构建完成之后的回调方法,而这个方法会受到@Order的顺序影响,效果前面日志中已经体现,这里贴一下类注释:
Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple CommandLineRunner beans can be defined within the same application context and can be ordered using the Ordered interface or @Order annotation.
除了以上两种用法,@Aspect声明的切面类、继承了OncePerRequestFilter的过滤器等,它们的作用顺序也会受到Order的影响。
注意:如果@Order注解配置在了@Configuration修饰的配置类中的@Bean方法修饰的方法上时,指定顺序并不会生效
顺便提一下另外一个注解:@Priority,以上@Order能起作用的地方,换成@Priority一样会生效,但在一种情况下,它的作用和@Order大为不同:
同一个接口类型有多个不同的bean实现类时,注入依赖时使用集合声明不会报错,但声明为单体类型时,如果各个Bean类使用了@Order声明,就会报required a single bean, but x were found的错误,这时有两种方法可以解决问题,一是在其中一个Bean类加上@Primary的注解声明为首要类型,另外一个就是把Order改成Priority,优先级最高的那个bean会被当作primary来对待。
到此这篇关于Springboot中的@Order如何使用的文章就介绍到这了,更多相关Springboot @Order内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring Boot 3.4.0 结合 Mybatis-plus 实
本文详细介绍了在 Spring Boot 3.4.0 项目中结合 Mybatis-plus 实现动态数据源切换的完整方案,通过自定义注解和AOP切面,我们可以优雅地实现方法级别的数据源切换,满足多数据源场景下的各种需求,感兴趣的朋友一起看看吧2025-04-04
springboot整合mybatis的超详细过程(配置模式+注解模式)
这篇文章主要介绍了springboot整合mybatis的详细过程(配置模式+注解模式),这里我使用的是配置模式+注解模式所以需要配置全局文件,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下2022-04-04
详解Spring Boot中@PostConstruct的使用示例代码
在Java中,@PostConstruct是一个注解,通常用于标记一个方法,它表示该方法在类实例化之后(通过构造函数创建对象之后)立即执行,这篇文章主要介绍了详解Spring Boot中@PostConstruct的使用,需要的朋友可以参考下2023-09-09
解读SpringBoot接收List<Bean>参数问题(POST请求方式)
这篇文章主要介绍了解读SpringBoot接收List<Bean>参数问题(POST请求方式),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-09-09


最新评论