<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.7.RELEASEversion>
<relativePath/>
parent>
<groupId>com.xpfgroupId>
<artifactId>demoautoconfigureartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demoautoconfigurename>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
<version>2.1.3.RELEASEversion>
dependency>
dependencies>
project>
package com.xpf.demoautoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @Title:UserServiceProperties
* @Package:com.xpf.demoautoconfigure
* @Author: xiapf
* @Date:2019/8/28
* @Descrption: 属性配置
*/
@ConfigurationProperties(prefix = "user.name")
public class UserServiceProperties {
private static final String NAME = "xpf";
private String name = NAME;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.xpf.demoautoconfigure;
/**
* @Title:UserService
* @Package:com.xpf.demoautoconfigure
* @Author: xiapf
* @Date:2019/8/28
* @Descrption: 判断依据类(根据此类的存在与否来创建这个类的Bean)
*/
public class UserService {
private String name;
public String getUserName() {
return "用户姓名:" + name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.xpf.demoautoconfigure;
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;
/**
* @Title:UserServiceAutoConfiguration
* @Package:com.xpf.demoautoconfigure
* @Author: xiapf
* @Date:2019/8/28
* @Descrption: 自动配置类
*/
@Configuration
@EnableConfigurationProperties(UserServiceProperties.class)
@ConditionalOnClass(UserService.class)
@ConditionalOnProperty(prefix = "user.name", value = "enabled", matchIfMissing = true)
public class UserServiceAutoConfiguration {
@Autowired
private UserServiceProperties userServiceProperties;
@Bean
@ConditionalOnMissingBean(UserService.class)
public UserService userService() {
UserService userService = new UserService();
userService.setName(userServiceProperties.getName());
return userService;
}
}
@EnableConfigurationProperties(UserServiceProperties.class) :开启属性注入,通过@EnableConfigurationProperties声明, 使用 @Autowired注入
@ConditionalOnClass(UserService.class) :当UserService在类路径的条件下
@ConditionalOnProperty(prefix = “user.name”,value = “enabled”,matchIfMissing = true):
当设置user.name=enabled的情况下,如果没有设置则默认为true,即条件符合
@Bean :像使用java配置的方式配置UserService这个Bean
@ConditionalOnMissingBean(UserService.class): 当容器中没有这个Bean的时候新建Bean
若想自动配置生效,需要注册自动配置类,在src/main/resources下新建META-INF/spring.factories
spring.factories内容如下
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xpf.demoautoconfigure.UserServiceAutoConfiguration
新建Spring Boot项目,将自定义starter作为依赖,修改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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.7.RELEASEversion>
<relativePath/>
parent>
<groupId>com.xpfgroupId>
<artifactId>testdemoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>testdemoname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.xpfgroupId>
<artifactId>demoautoconfigureartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
package com.xpf.testdemo;
import com.xpf.demoautoconfigure.UserService;
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;
@SpringBootApplication
@RestController
public class TestdemoApplication {
@Autowired
private UserService userService;
@RequestMapping("/")
public String index(){
return userService.getUserName();
}
public static void main(String[] args) {
SpringApplication.run(TestdemoApplication.class, args);
}
}
启动项目,打开浏览器,输入http://localhost:8080/,效果:
Spring Boot自动配置原理