最近在做传统Spring项目到SpringBoot项目迁移过程中,遇到了一些bean加载顺序的问题:
比如一个config中的bean依赖于另一个config中的bean进行初始化,于是查了一些资料,出现了一些新的概念:
@Order@AutoConfigureAfter@DependsOn
@Order注解
Before Spring 4.0, the
@Orderannotation was used only for theAspectJexecution order. It means the highest order advice will run first.
Since Spring 4.0, it supports the ordering of injected components to a collection. As a result, Spring will inject the auto-wired beans of the same type based on their order value.
在Spring 4.0版本之前,@Order注解只能控制AOP的执行顺序,在Spring 4.0之后,它还可以控制集合注入中bean的顺序。
控制AOP顺序很好理解,例如可以在@Aspect注解的切面上加入@Order注解,控制切面的执行顺序。
还有@EnableTransactionManagement(order = 10),这种写法,由于Spring的事务也是用AOP实现,也可以控制优先级。
下面举个例子说明控制集合注入中bean的顺序。
|
|
最后是测试类:
如果不使用@Order注解,那ratings集合可能是乱序的。
有一种错误的用法:
先定义两个service:
然后写两个config注入bean:
本意是想通过@Order控制bean的注入顺序,先注入orderService2,再注入orderService1。但是并没有效果。
所以,@Order注解放到@Configuration中是无法控制bean的注入顺序的。
@AutoConfigureAfter注解
Hint for that an
EnableAutoConfigurationauto-configuration should be applied after other specified auto-configuration classes.
类似的注解还有:
@AutoConfigureBefore@AutoConfigureOrder
这三个注解是特地用于autoconfigure类的,不能用于普通的配置类。
有必要先说明一下autoconfigure类项目。
通常我们会在主类入口上标注@SpringBootApplication注解,或者直接标注@EnableAutoConfiguration注解。
这个注解是用来根据类路径中的依赖包猜测需要注入的bean,实现自动注入:
Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined.
可以理解为,@EnableAutoConfiguration是服务于自动注入的bean的,即spring-boot-starter中bean的自动加载顺序。
被排序的这些类,都是通过xxx-spring-boot-autoconfigure项目中的src/resources/META-INF/spring.factories配置文件获取的,这个文件中的配置内容一般为:
Spring Boot 只会对从这个文件读取的配置类进行排序。
但是不要以为将自己的配置类也配置在spring.factories中就能实现排序,如果你的类被自己Spring Boot启动类扫描到了,这个类的顺序会优先于所有通过spring.factories读取的配置类。
Auto-configuration is always applied after user-defined beans have been registered.
@DependsOn注解
Beans on which the current bean depends. Any beans specified are guaranteed to be created by the container before this bean.
Used infrequently in cases where a bean does not explicitly depend on another through properties or constructor arguments, but rather depends on the side effects of another bean’s initialization.
May be used on any class directly or indirectly annotated with org.springframework.stereotype.Component or on methods annotated with Bean.
Using DependsOn at the class level has no effect unless component-scanning is being used. If a DependsOn-annotated class is declared via XML, DependsOn annotation metadata is ignored, andis respected instead.
从java doc中可以看出,@DependsOn注解可以用来控制bean的创建顺序,该注解用于声明当前bean依赖于另外一个bean。所依赖的bean会被容器确保在当前bean实例化之前被实例化。
一般用在一个bean没有通过属性或者构造函数参数显式依赖另外一个bean,但实际上会使用到那个bean或者那个bean产生的某些结果的情况。
用法
- 直接或者间接标注在带有
@Component注解的类上面; - 直接或者间接标注在带有
@Bean注解的方法上面; - 使用
@DependsOn注解到类层面仅仅在使用 component-scanning 方式时才有效;如果带有@DependsOn注解的类通过XML方式使用,该注解会被忽略,<bean depends-on="..."/>这种方式会生效。
例如,我们有一个FileProcessor依赖于FileReader和FileWriter,FileReader和FileWriter需要在FileProcessor之前初始化:
也可以在Component上标注:
属性注入和构造器注入
上面说到@DependsOn注解时提到,它一般用在一个bean没有通过属性或者构造函数参数显式依赖另外一个bean,但实际上会使用到那个bean或者那个bean产生的某些结果的情况。
如果bean直接依赖于另一个bean,我们可以将其通过属性或者构造函数引入进来。
而使用构造函数的方法显示依赖一个bean,能够保证被依赖的bean先初始化。但是属性注入不可以。
constructor-injection automatically enforces the order and completeness of the instantiated.
因此,我们可以在Component中使用构造函数显示注入依赖的bean:
注意,需要使用@Qualifier限定bean名称时,不能标注在构造方法上,而是应该标注在参数上。原因跟@Resource不能标注构造方法一样,它不知道你要限定哪个参数。
假设有两个service,OrderService1依赖于OrderService2:
对应的Configuration:
输出:
可以看出,OrderService2先初始化。
换一种OrderService1写法,使用属性注入:
输出:
可以看出,OrderService2并没有先初始化。
当然,OrderService1Config也可以使用构造器注入:
参考:
@Order in Spring
Controlling Bean Creation Order with @DependsOn Annotation