1.什么是Starter?
Starter可以理解为一个可拔插式的插件,提供一系列便利的依赖描述符,您可以获得所需的所有Spring和相关技术的一站式服务.应用程序只需要在maven中引入Starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置.用一句话描述,starter就是SpringBoot的场景启动器;
2.SpringBoot的AutoConfiguration机制
标记一个应用为SpringBoot应用,需要一个SpringBootApplication
注解,下面是一个标准的SpringBoot启动程序;
/**
* Description SpringBoot启动类
*
* @author Peng
* @date 2020/11/7
*/
@SpringBootApplication
public class SpringBootDemoApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApp.class,args);
}
}
SpringBootApplication
注解中又有一个EnableAutoConfiguration注解,EnableAutoConfiguration
注解就是自动加载配置的关键;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
//省略类中的代码...
}
EnableAutoConfiguration
注解是一个组合注解,用Import把AutoConfigurationImportSelector导入容器中,SpringBoot启动的时候会加载所有的selector并执行selectImports方法,这个方法会加载META-INF/spring.factories
中配置的EnableAutoConfiguration,从而加载自动配置;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
//省略类中的代码...
}
3.创建自己的Starter
一个完整的SpringBoot Starter可能包含以下组件:
autoconfigure
模块:包含自动配置的代码;
starter
模块:提供对autoconfigure
模块的依赖以及一些其它的依赖;
注意:如果你不需要区分这两个概念的话,也可以将自动配置代码模块与依赖管理模块合并成一个模块;
简而言之,Starter应该提供使用该库所需的一切!
3.1.命名规范
①.模块名称不能以
spring-boot
关键字开头;
②.如果你的Starter提供了配置keys,那么请确保它们有唯一的命名空间.而且不要用SpringBoot用到的命名空间(比如:server,management,spring等等);
举个例子,假设你为acme
创建了一个Starter,那么你的auto-configure模块可以命名为acme-spring-boot-autoconfigure
,Starter模块可以命名为acme-spring-boot-starter
.如果你只有一个模块包含这两部分,那么你可以命名为acme-spring-boot-starter
;
3.2.autoconfigure模块
需要在autoconfigure模块中包含下列依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>
3.3.Starter模块
事实上,starter是一个空jar
,它唯一的目的是提供这个库所必须的依赖,你的starter必须直接或间接引用核心的SpringBoot Starter(spring-boot-starter)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
4.自定义Starter
4.1.新建一个Maven项目命名为hello-spring-boot-starter
1>.pom.xml
<?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.xp</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello-spring-boot-starter</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
2>.HelloProperties.java
/**
* Description 属性配置类
* 该类中的属性可以本项目中设置默认值,也可以在后期引用的项目中的配置文件中自定义属性值然后覆盖默认值!
*
* @author Peng
* @date 2020/11/7
*/