demo地址:https://github.com/zackzhangCN/SpringBootStarterDemo.git
(一)自定义Starter
1.创建maven项目
2.引入自动配置maven依赖
<?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>cn.zack</groupId>
<artifactId>helloworld-spring-boot-starter</artifactId>
<version>1.0.0-RELEASE</version>
<packaging>jar</packaging>
<name>helloworld-spring-boot-starter</name>
<description>自定义starter</description>
<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-autoconfigure</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.0.0.RELEASE</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.编写spring.factories配置文件
指定自动装配配置类路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.zack.UserServiceAutoConfiguration
4.编写UserProperties类
指定从配置文件中读取属性进行装配的前缀
package cn.zack;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.user")
public class UserProperties {
private String username;
private int age;
private String gender;
// todo get set constructor
}
5.编写UserService业务类
package cn.zack;
public class UserService {
private UserProperties userProperties;
public UserService() {
}
public UserService(UserProperties userProperties) {
this.userProperties = userProperties;
}
public void hello(){
System.out.println(userProperties.getUsername() + "\n" + userProperties.getAge() + "\n" + userProperties.getGender());
}
}
6.编写自动装配配置类UserServiceAutoConfiguration
package cn.zack;
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;
/**
* ConditionalOnClass 当类路径classpath下有指定的类的情况下进行自动配置
* ConditionalOnProperty 当配置文件中example.service.enabled=true时进行自动配置,如果没有设置此值就默认使用matchIfMissing对应的值
* ConditionalOnBean 当容器(Spring Context)中有指定的Bean的条件下
* ConditionalOnMissingBean 当容器(Spring Context)中没有指定Bean的情况下进行自动配置
*/
@Configuration
@EnableConfigurationProperties(UserProperties.class)
@ConditionalOnClass(UserService.class)
@ConditionalOnProperty(prefix = "spring.user", value = "enabled", matchIfMissing = true)
public class UserServiceAutoConfiguration {
@Autowired
private UserProperties userProperties;
@Bean
@ConditionalOnMissingBean(UserService.class)
public UserService userService(){
UserService userService = new UserService(userProperties);
return userService;
}
}
7.maven打包
mvn clean install -DskipTests
(二)使用自定义Starter
1.新建一个springboot项目, 引入自定义starter的maven依赖
<dependency>
<groupId>cn.zack</groupId>
<artifactId>helloworld-spring-boot-starter</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
2.yml配置文件中配置自定义starter的属性
3.注入自定义starter的UserService
@Autowired
UserService userService;
@Test
public void test() {
userService.hello();
}