常用注解
@Configuration
指定这个类是一个配置类
@ConditionalOnClass
当类路径classpath下有指定的类的情况下进行自动配置
@ConditionalOnMissingClass
当类路径下没有指定的类的条件下
@ConditionalOnBean
当容器(Spring Context)中有指定的Bean的条件下
@ConditionalOnMissingBean
当容器(Spring Context)中没有指定Bean的情况下进行自动配置
@ConfigurationProperties
结合相关XxxProperties来绑定相关配置, 主要用来把properties配置文件转化为对应的XxxProperties来使用的, 并不会把该类放入到IOC容器中
@EnableConfigurationProperties(XxxProperties.class)
注解的作用是使@ConfigurationProperties注解生效并且将XxxProperties注入到IOC容器中。
如果在每个Properties上都使用@Component来标注,那么也不需要使用@EnableConfigurationProperties({XxxProperties.class})来标注了,同样也可以在Spring上下文容器中也能获取到XxxProperties对应的Bean。
@ConditionalOnProperty(prefix = “spring.person”, value = “enabled”, matchIfMissing = true)
当配置文件中spring.person.enabled=true时进行自动配置,如果没有设置此值就默认使用matchIfMissing对应的值。如果不设置matchIfMissing = true, 默认为false。
META-INF/spring.factories
自动配置能加载,需要将自动配置类放在META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
xxx.xxx.xxxAutoConfiguration,\
aaa.aaa.aaaAutoConfiguration
编写starter
pom依赖
<?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 https://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.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.springboot</groupId>
<artifactId>lzc-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lzc-spring-boot-starter</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
PersonProperties.java
@ConfigurationProperties(prefix = "spring.person")
public class PersonProperties {
// 姓名
private String name;
// 年龄
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
PersonService.java
public class PersonService {
private PersonProperties properties;
public PersonService() {
}
public PersonService(PersonProperties properties) {
this.properties = properties;
}
public void sayHello(){
System.out.println("name = " + properties.getName() + ", age = " + properties.getAge());
}
}
PersonServiceAutoConfiguration.java
@Configuration
@EnableConfigurationProperties(PersonProperties.class)
@ConditionalOnClass(PersonService.class)
@ConditionalOnProperty(prefix = "spring.person", value = "enabled", matchIfMissing = true)
public class PersonServiceAutoConfiguration {
@Autowired
private PersonProperties properties;
@Bean
@ConditionalOnMissingBean(PersonService.class) // 当容器中没有指定Bean的情况下,自动配置PersonService类
public PersonService personService(){
PersonService personService = new PersonService(properties);
return personService;
}
}
最后使用maven命令:mvn install, 打包到本地仓库
在其它工程中引入依赖包
<dependency>
<groupId>com.example.springboot</groupId>
<artifactId>lzc-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>