创建自定义starter的时候需要考虑的问题:

1.这个starter需要哪些依赖

2.如何编写自动配置

@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件

@ConfigurationPropertie // 结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties // 让xxxProperties生效加入到容器中

// 自动配置类要能加载
// 将需要启动就加载的自动配置类,配置在META‐INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\

3.模式

启动器只用来做依赖导入,需要写一个自动配置模块,启动器依赖自动配置模块,在使用自定义启动器的时候,只需要导入启动器即可,使用“自定义启动器-spring-boot-starter”的方式命名。

步骤:

创建一个Empty Project项目。

添加Modules,选择New Module,添加一个maven项目,再添加一个Spring Initializr项目(不引入任何模块)。

Spring Boot笔记-Spring Boot自定义starters(八)_xml

Spring Boot笔记-Spring Boot自定义starters(八)_xml_02

删掉atguigu-spring-boot-starter-autoconfigurer的主启动类、测试类和application.properties文件,在atguigu-spring-boot-starter的pom.xml里加上atguigu-spring-boot-starter-autoconfigurer的坐标。删除atguigu-spring-boot-starter-autoconfigurer的pom.xml中多余的依赖,只保留spring-boot-start坐标。

<?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.atguigu.starter</groupId>
<artifactId>atguigu-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--引入自动配置模块-->
<dependency>
<groupId>com.atguigu.starter</groupId>
<artifactId>atguigu-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
<?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.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.atguigu.starter</groupId>
<artifactId>atguigu-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>atguigu-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入spring-boot-starter:所有的starter都需要的基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

HelloProperties.java:看做HelloService组件对应的属性。

package com.atguigu.starter;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "atguigu.hello")
public class HelloProperties {
private String prefix;
private String suffix;

public String getPrefix() {
return prefix;
}

public void setPrefix(String prefix) {
this.prefix = prefix;
}

public String getSuffix() {
return suffix;
}

public void setSuffix(String suffix) {
this.suffix = suffix;
}
}

HelloService.java:看做一个功能组件,组件中用到的属性是和HelloProperties绑定的。

package com.atguigu.starter;

public class HelloService {
private HelloProperties helloProperties;

public HelloProperties getHelloProperties() {
return helloProperties;
}

public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}

public String sayHello(String name) {
return helloProperties.getPrefix() + name + helloProperties.getSuffix();
}
}

HelloServiceAutoConfiguration.java:自动添加HelloService组件到容器中。

package com.atguigu.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;

@Bean
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}

在resources下创建META-INF文件夹,加入spring.factories文件,这样在启动的时候,才会加载这个自动配置。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.atguigu.starter.HelloServiceAutoConfiguration

此时,自动配置包就完成了,将它添加到maven仓库中,方便其他人来使用。

打开idea的maven标签,找到atguigu-spring-boot-starter-autoconfigurer和atguigu-spring-boot-starter,运行Lifecycle下的install,安装到maven本地仓库,注意,因为atguigu-spring-boot-starter依赖atguigu-spring-boot-starter-autoconfigurer,所以先安装atguigu-spring-boot-starter-autoconfigurer,后安装atguigu-spring-boot-starter。

新建一个Spring Boot项目做测试,选中Spring Web模块。在pom.xml中引入自定义的starter。

<dependency>
<groupId>com.atguigu.starter</groupId>
<artifactId>atguigu-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

编写HelloController.java用于测试。

package com.atguigu.springboot.controller;

import com.atguigu.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
@Autowired
HelloService helloService;

@GetMapping("/hello")
public String hello() {
return helloService.sayHello("hello");
}
}

通过在application.properties或application.yml中添加配置实现属性的自动赋值,发送​​http://localhost:8080/hello​​请求查看效果。

application.properties
atguigu.hello.prefix=PREFIX
atguigu.hello.suffix=SUFFIX

application.yml
atguigu.hello.prefix: PREFIX
atguigu.hello.suffix: SUFFIX