EnableAutoConfiguration注解功能是启动自动配置,SpringBootApplication注解包含EnableAutoConfiguration,所以在启动类上添加SpringBootApplication可以启动自动配置。如图:
该注解可以设置两个属性,exclude和excludeName根据类型或者类的名字排除bean,如下:
第83行spring.boot.enableautoconfiguration是指在application.properties中其值要为true(默认true),否则不会自动配置。该注解的导入类
AutoConfigurationImportSelector的selectImports方法如下:
首先判断application.properties中spring.boot.enableautoconfiguration是否为true,后面调用getCandidateConfigurations方法,如下:
红框标记出在META-INF/spring.factories文件中的配置要加载。请看下面示例:
1、新建项目core-bean
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>com.edu.core</groupId> <artifactId>core-bean</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>springboot</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> </dependencies> </project>
Role.java
package com.edu.core.bean; public class Role { }
User.java
package com.edu.core.bean; public class User { }
RunnableConfiguration.java
package com.edu.core.bean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RunnableConfiguration { @Bean public Runnable createRunnable(){ return () -> {}; } }
UserConfiguration.java
package com.edu.core.bean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class UserConfiguration { @Bean public User createUser(){ return new User(); } }
resources/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.edu.core.bean.RunnableConfiguration,com.edu.core.bean.UserConfiguration,com.edu.core.bean.Role
2、新建项目test
pom.xml,要把上面项目的依赖加进来
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>com.edu.spring</groupId> <artifactId>springboot</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>springboot</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.6.RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.edu.core</groupId> <artifactId>core-bean</artifactId> <version>1.0.0</version> </dependency> </dependencies> </project>
App.java
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import com.edu.core.bean.Role; import com.edu.core.bean.User; @EnableAutoConfiguration @ComponentScan public class App { public static void main( String[] args ) { ConfigurableApplicationContext context=SpringApplication.run(App.class,args); System.out.println(context.getBean(Runnable.class)); System.out.println(context.getBean(User.class)); System.out.println(context.getBean(Role.class)); context.close(); } }
运行结果如下:
可以看到,通过在项目core-bean的resources/META-INF/spring.factories中设置相关属性,其他项目便可装配bean。
在test项目的application.properties中设置spring.boot.enableautoconfiguration=false,运行无法获取bean,如下:
根据类名排除装配bean:
App.java
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import com.edu.core.bean.User; @EnableAutoConfiguration(excludeName= {"excludeName","com.edu.core.bean.RunnableConfiguration"}) @ComponentScan public class App { public static void main( String[] args ) { ConfigurableApplicationContext context=SpringApplication.run(App.class,args); //System.out.println(context.getBean(Runnable.class)); System.out.println(context.getBean(User.class)); //System.out.println(context.getBean(Role.class)); context.close(); } }
根据类排除装配bean:
App.java
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import com.edu.core.bean.Role; import com.edu.core.bean.RunnableConfiguration; import com.edu.core.bean.User; @EnableAutoConfiguration(exclude= {Role.class,RunnableConfiguration.class}) @ComponentScan public class App { public static void main( String[] args ) { ConfigurableApplicationContext context=SpringApplication.run(App.class,args); //System.out.println(context.getBean(Runnable.class)); System.out.println(context.getBean(User.class)); //System.out.println(context.getBean(Role.class)); context.close(); } }
以上两种配置都可以排除bean,需要注释掉获取相关bean的代码,否则程序报错。
总结: (1)EnableAutoConfiguration 从classpath搜索所有META-INF/spring.factories配置文件,将其中的配置项装配成bean,加载到spring容器
(2)application.properties中,spring.boot.enableautoconfiguration=true 时才生效,默认true
(3)可以使用exclude或者excludeName根据类型或者类名排除不需要的bean