1. Starter 介绍
1.1. 作用
- 启动器(starter)包含许多依赖项,这些依赖项是使项目快速启动和运行所需的依赖项。
例如通过配置 spring-boot-starter-data-redis,可以快捷地使用 Spring 对 Redis 进行数据访问。
1.2. 命名规范
官方提供的 starter 遵循类似的命名规范:spring-boot-starter-*。
第三方 starter 命名应当遵循 thirdpartyproject-spring-boot-latest。
1.3. 常用 starter
spring-boot-starter-jdbc
- spring-boot-starter-data-redis
- spring-boot-starter-web
- spring-boot-starter-actuator
2. Web 开发示例
- 引入
spring-boot-starter-web
实现快速引入和启动,无需再进行烦杂的 xml 配置。 - 默认基于 Tomcat 容器运行,可通过修改 pom.xml 指定运行的容器。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
3. 自研 Starter 的步骤
- 建工程。
- 引入
spring-boot-starter
、spring-boot-autoconfigue
、第三方 jar。 - 如需要生成配置元信息,加入
spring-boot-consiguration-processon
依赖。 - 编写自动配置类。
- 配置发现配置文件(
META-INF/spring.factories
)。 - 打包发布。
4. 自研 Starter 示例
4.1. 创建一个 保存属性的项目(girl)
- 项目结构
pom.xml ```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">
4.0.0 <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository -->
com.learn girl 1.0.0 girl 1.8 org.projectlombok lombok provided
- GirlDemo
```java
@Data
public class GirlDemo {
private String name;
private String length;
private String face;
}
4.2. 创建一个自定义的 starter(girl-spring-boot-starter)
- 项目结构
pom.xml ```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">
4.0.0 <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository -->
com.learn girl-spring-boot-starter 1.0.0 girl-spring-boot-starter 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-autoconfigure org.springframework.boot spring-boot-configuration-processor true com.learn girl 1.0.0 org.projectlombok lombok provided META-INF META-INF/
- GirlProperties
```java
@Data
@ConfigurationProperties(prefix = "com.girl")
public class GirlProperties {
private String name;
private String length;
private String face;
}
GirlAutoConfigure ```java @Configuration @EnableConfigurationProperties(GirlProperties.class) public class GirlAutoConfigure {
@Bean public GirlDemo getGirl(GirlProperties properties) {
GirlDemo g = new GirlDemo(); g.setName(properties.getName()); g.setLength(properties.getLength()); g.setFace(properties.getFace()); return g;
}
}
- spring.factories
```properties
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.learn.girl.autoconfigure.GirlAutoconfigure
4.3. 项目中使用自定义的 starter
添加依赖(pom.xml)
<dependency> <groupId>com.learn</groupId> <artifactId>girl-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency>
添加配置(application.yml)
com: girl: name: test length: 180 face: 111
测试 ```java @RestController public class MyController {
@Autowired private GirlDemo girlDemo;
@GetMapping(“index”) public String index() {
return girlDemo.toString();
}
} ```