文章目录
简介
官方文档中关于自定义starter的描述
4.29.5. Creating Your Own Starter
Concretely, a custom starter can contain the following:
• The autoconfigure module that contains the auto-configuration code for “acme”.
• The starter module that provides a dependency to the autoconfigure module as well as “acme” and any additional dependencies that are typically useful. In a nutshell, adding the starter should provide everything needed to start using that library.
- autoconfigure module 代码写在此moudle中
- starter module只是一个空的项目,它的唯一目的是提供必要的依赖
如果配置简单,没有可选项,可以把所有东西都写在一个Moudle中
使用Spring Initializr创建一个项目
SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。
删掉一些不需要的东西,项目结构和pom如下:
<?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.xgsama</groupId>
<artifactId>xg-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>
定义一个配置信息映射类
package com.xgsama.starter.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
// 该注解有一个prefix属性,通过指定的前缀,绑定配置文件中的配置
@ConfigurationProperties("xg.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;
}
}
定义一个Service
package com.xgsama.starter.service;
import com.xgsama.starter.autoconfigure.HelloProperties;
import javax.annotation.Resource;
/**
* HelloService
*
* @author xgSama
* @date 2021/1/10 22:59
*/
public class HelloService {
@Resource
HelloProperties properties;
public String sayHello(String name) {
return properties.getPrefix() + ":" + name + "!" + properties.getSuffix();
}
}
定义一个配置类自动装配Service
@EnableConfigurationProperties
使 @ConfigurationProperties 注解的类生效
@ConditionalOnMissingBean
指定在某个Bean不存在时配置才生效
package com.xgsama.starter.autoconfigure;
import com.xgsama.starter.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* HelloServiceAutoConfiguration
*
* @author xgSama
* @date 2021/1/10 22:59
*/
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService() {
return new HelloService();
}
}
在spring.factories中指定自动装配的类
在classpath下创建META-INF,新建spring.factories文件
添加以下内容,指定上步骤中定义的配置类为自动装配的配置。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xgsama.starter.autoconfigure.HelloServiceAutoConfiguration
安装测试
执行maven clean install将自定义的starter安装到本地maven仓库,新建一个Spring Boot测试这个starter
引入依赖
<dependency>
<groupId>com.xgsama</groupId>
<artifactId>xg-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
添加配置
server:
port: 8888
xg:
hello:
prefix: hello
suffix: nice to meet you
编写测试Controller
/**
* AlphaController
*
* @author xgSama
* @date 2020/12/26 10:44
*/
@RestController
public class AlphaController {
@Resource
HelloService helloService;
@GetMapping("/hello")
public String hello() {
String sayHello = helloService.sayHello("玄戈");
return sayHello;
}
}
启动项目测试