1. 自动配置原理:
spring boot项目的入口类的注解@SpringBootApplication引入了@EnableAutoConfiguration。后者又通过@Import引入了EnableAutoConfigurationImportSelector类, EnableAutoConfigurationImportSelector类通过方法调用链selectImports()->getCandidateConfigurations->SpringFactoriesLoader.loadFactoryNames(),获得了在spring-boot-autoconfig jar包中META-INF/spring.factories文件的org.springframework.boot.autoconfigure.EnableAutoConfiguration属性所要求的所有需要自动配置的AutoConfiguration。这些自动配置的AutoConfiguration一般由@Configuration注解,由其同一目录下的相关基于属性的Bean定义属性,并且在这些AutoConfiguration中包含了返回Bean的由@Conditional+条件进行注解的方法,因此在项目中可以通过@Autowired、@Resource等方式直接进行注入。(以上原理说明均由spring boot源代码得到)。
2. 示例的步骤:
步骤一:建立自动配置的maven组件包。该包模拟autoconfig组件建立了基于properties的安全类型的Bean(即@ConfigurationProperties注解的Bean),应用该Bean的自动配置的AutoConfiguration(由@Configuration注解,使用上一Bean的属性),AutoConfiguration中包含应用上一Bean属性的服务Bean, 并在资源目录下建立META-INF/spring.factories文件,文件中为org.springframework.boot.autoconfigure.EnableAutoConfiguration属性确定了AutoCOnfiguration的全限定名。最后按照组件包(mvn:install)。
步骤二:建立一个依赖web的spring boot项目。项目还依赖了上面建立的maven组件,在入口类中通过@Autowired注入maven组件中的服务Bean,再在入口类的@RequestMapping注解的方法中使用该服务Bean,从而验证maven组件自动配置的正确性。
3. 示例
步骤一:
demo-autoconfig maven组件的pom.xml
依赖autoconfig组件的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>
<groupId>com.example.demo</groupId>
<artifactId>demo-autoconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo-autoconfig</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>2.0.2.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<!-- autoconfigure support -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
基于properties的安全类型的Bean
package com.example.demo.propertybean;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "hello")
public class DemoPropertyBean {
private static final String MSG = "world";
private String msg = MSG;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
服务Bean
package com.example.demo.condition;
/** Existence of this class under the class path acts as a condition.
* Which is used in the class annotated with @ConditionalOnClass
* @author apple
*
*/
public class DemoService {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String saySomething() {
return "Hello, " + this.msg;
}
}
AutoConfiguration
package com.example.demo.autoconfigclass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.demo.condition.DemoService;
import com.example.demo.propertybean.DemoPropertyBean;
// check if class DemoService is under class path.
@ConditionalOnClass(DemoService.class)
// if property "hello.msg" in application.property is missing, condition is matched.
@ConditionalOnProperty(prefix="hello", value="msg", matchIfMissing=true)
// enable inject the properties from DemoPropertyBean
@EnableConfigurationProperties(DemoPropertyBean.class)
@Configuration
public class DemoAutoConfiguration {
@Autowired
DemoPropertyBean demoPropertyBean;
@Bean
@ConditionalOnMissingBean(DemoService.class)
public DemoService demoService() {
DemoService demoService = new DemoService();
demoService.setMsg(demoPropertyBean.getMsg());
return demoService;
}
}
spring.factories文件
在资源目录src/main/resources下建立META-INF/spring.factories文件,文件内容为:
org.springframework.boot.autoconfig.EnableAutoConfiguration=\
com.example.demo.autoconfigclass.DemoAutoConfiguration
属性值为需要自动配置的AutoConfiguration。
步骤二:
spring boot项目的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.example</groupId>
<artifactId>com-use-demo-autoconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>com-use-demo-autoconfig</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- demo autoconfig support -->
<dependency>
<groupId>com.example.demo</groupId>
<artifactId>demo-autoconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
入口类
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.condition.DemoService;
@RestController
@SpringBootApplication
public class ComUseDemoAutoconfigApplication {
/**inject demo-autoconfig组件的DemoService
*
*/
@Autowired
DemoService demoService;
@RequestMapping({"/", "/index"})
public String index() {
return demoService.saySomething();
}
public static void main(String[] args) {
SpringApplication.run(ComUseDemoAutoconfigApplication.class, args);
}
}
验证结果
a、运行spring boot项目
b、地址栏输入:http://localhost:8080/index
c、浏览器显示:
Hello, world
d、修改spring boot项目的application.properties文件,增加如下一行:
hello.msg=Hongkong
e、再次运行spring boot项目浏览器显示:
Hello, Hongkong
f、因此demo-autoconfig自动配置验证成功。