Spring-Boot-原理-自定义starter
前言:spring-boot最强大的地方在于将所有的场景均抽取成了starter,也就是场景启动器,通过引入场景启动器就能使用其功能,即使是这样也不能完全保证后期的需求,所以自定义的场景启动器就很重要。而且当自己编辑好starter的时候,其他人也可以通过导入依赖的方式直接使用。
- 创建空白项目,在此项目中分别加入使用maven从创建项目和使用Spring Initializr创建项目的两个module。此处以maven创建的模块com.atguigu.starter只当作场景启动器,而以Spring Initializr创建的模块atguigu-spring-boot-starter-autoconfigurer作为自动配置。
- 创建好项目后,将atguigu-spring-boot-starter-autoconfigurer模块中多余的目录以及依赖删除(不需要使用的那些加载器),在com.atguigu.starter模块中导入atguigu-spring-boot-starter-autoconfigurer模块的坐标:如下:
<?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>
<!--启动器-->
<dependencies>
<!--引入自动配置模块-->
<dependency>
<groupId>com.atguigu.starter</groupId>
<artifactId>atguigu-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
需要注意的是atguigu-spring-boot-starter-autoconfigurer模块中的依赖需要导入spring-boot的场景启动器:具体如下:
<?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.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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:所有spring-boot-starter都需要引入的场景-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
- 编写功能类,此处为HelloService,主要方法为输出名字具体如下,想要通过配置文件中定义一些属性,通过此类做一些操作,所以还需要编写与配置信息绑定的properties类
package com.atguigu.starter;
public class HelloService {
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();
}
}
- HelloProperties类如下:此处需要注意的是@ConfigurationProperties(prefix = “atguigu.hello”)注解,此注解为结合相关配置来绑定信息:
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;
}
}
- 还需要自动配置类,所以来编写自动配置类HelloServiceAutoConfigration,具体如下:因为需要加入容器中,所以加上@Conponent注解,而@EnableConfigurationProperties注解是为了使@ConfigurationProperties注解生效。
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.stereotype.Component;
@Component
@ConditionalOnWebApplication//web应用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfigration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
- 将自动配置类配置在MATE/INF下的spring.factories配置文件中,如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.atguigu.starter.HelloServiceAutoConfigration
项目的文件目录如下:
6.最后先将自动配置类的项目通过maven-install加载到maven的仓库中,再将启动器的模块install到maven的仓库中。新建一个项目测试即可。因为启动器中依赖自动配置类,所以要先install自动配置类。