如果项目中要开发一个比较通用的组件,这个组件要被其它springboot项目应用并且其它springboot项目在应用它时不想要加入新的标注和代码,只要在pom.xml里加入它的依赖就。这种情况下,怎么办呢
假如:我们组件名字时spring_autoconfiguration,在我们的组件里加入META-INF/spring.factories文件
创建这个组件的MyEnableAutoConfiguration
- @Configuration(proxyBeanMethods = false)
- public class MyEnableAutoConfiguration {
-
- static Logger logger = LoggerFactory.getLogger(MyEnableAutoConfiguration.class);
-
- public MyEnableAutoConfiguration() {
- logger.info("MyEnableAutoConfiguration is starting instantiating");
- }
-
- @Bean
- public MyService myService() {
- return new MyService();
- }
-
- }
MyService
- public class MyService {
-
- }
pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.6.2</version>
- <relativePath />
- </parent>
-
- <groupId>com</groupId>
- <artifactId>spring_autoconfiguration</artifactId>
- <version>1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
- </project>
这个是springboot的项目,需要依赖组件spring_autoconfiguration,
pom.xml
- <dependency>
- <groupId>com</groupId>
- <artifactId>spring_autoconfiguration</artifactId>
- <version>1-SNAPSHOT</version>
- </dependency>
spring boot 的启动类
- @SpringBootApplication
- public class App {
-
- static Logger logger = LoggerFactory.getLogger(App.class);
-
- public static void main(String[] args) {
- ConfigurableApplicationContext context = SpringApplication.run( App.class, args );
- MyService myService = context.getBean(MyService.class);
- logger.info("myService is " + myService);
- }
- }
执行代码
2022-06-27 18:30:22.081 INFO 16168 --- [ main] com.harry.App : myService is com.harry.service.MyService@29a23c3d
在SpringBoot的启动类中,@SpringBootApplication注解的代码里面有一句@EnableAutoConfiguration
@ComponentScan: 注解的作用是扫描@SpringBootApplication所在的Application类(即spring-boot项目的入口类)所在的包(basepackage)下所有的@component注解(或拓展了@component的注解)标记的bean,并注册到spring容器中。
@EnableAutoConfiguration: 注解加载的是资源目录META-INF文件下的spring.factories的文件。包括导入到项目中的Jar包的META-INF文件夹下的spring.factories文件。spring.factories文件是一个properties文件。
组件的项目中的spring.factories文件的内容如下。也就是说,其它项目在导入我的这个Jar的时候,会去加载MyEnableAutoConfiguration这个类。多个类用逗号隔开
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- com.harry.autoconfiguration.MyEnableAutoConfiguration
如果你要改变某个AutoConfiugration的实例化的顺序,可以用下面的标注
@AutoConfigureAfter(value = {ConfigurationB.class})
或
@AutoConfigureBefore(value = {ConfigurationB.class})
例子代码
- @Configuration
- @AutoConfigureAfter(value = {ConfigurationB.class})
- public class ConfigurationA {
-
- public ConfigurationA() {
- System.out.println("ConfigurationA..");
- }
- }
- @Configuration
- public class ConfigurationB {
- public ConfigurationB() {
- System.out.println("ConfigurationB..");
- }
-
- }
在spring.factories文件里配置ConfigurationA和ConfigurationB
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- com.harry.configuration.ConfigurationA,\com.harry.configuration.ConfigurationB
执行结果
- ConfigurationB..
- ConfigurationA..
如果去掉@AutoConfigureAfter(value = {ConfigurationB.class})标注
- @Configuration
- //@AutoConfigureAfter(value = {ConfigurationB.class})
- public class ConfigurationA {
-
- public ConfigurationA() {
- System.out.println("ConfigurationA..");
- }
- }
执行结果
- ConfigurationA..
- ConfigurationB..
注意
如果没在spring.factories文件里配置的Configuration类不被视为AutoConfiguration.也就是说,配有@Configuration类如果没有在spring.factories配置的话,加@AutoConfigureAfter或@AutoConfigureBefore不能改变其实例化顺序。
比如去掉spring.factories的配置
- #org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- #com.harry.configuration.ConfigurationA,\com.harry.configuration.ConfigurationB
执行结果
- ConfigurationA..
- ConfigurationB..