Starter命名规则
官方命名:spirng-boot-starter-{name}
非官方命名:{name}-spring-boot-starter
自定义Starter编写
本次编写基于maven quikstart
初始的构建目录:
在此定义一个接口,功能是将任意类型转化成字符串。
package com.starter.format;
public interface FormatProcessor {
//定义一个格式化方法
<T> String format(T obj);
}
再定义两种实现,分别基于Json和String
package com.starter.format;
import com.alibaba.fastjson.JSON;
public class JsonFormatProcessor implements FormatProcessor {
@Override
public <T> String format(T obj) {
return "JsonFormatProcessor :"+ JSON.toJSONString(obj);
}
}
package com.starter.format;
import java.util.Objects;
public class StringFormatProcessor implements FormatProcessor{
@Override
public <T> String format(T obj) {
return "StringFormatProcessor: "+Objects.toString(obj);
}
}
接下来进行注入配置
@Configuration
public class FormatAutoConfiguration {
@ConditionalOnMissingClass("com.alibaba.fastjson.JSON")
@Bean
@Primary
public FormatProcessor stringFormat(){
return new StringFormatProcessor();
}
@ConditionalOnClass(name = "com.alibaba.fast.json.JSON")
@Bean
public FormatProcessor JsonFormat(){
return new JsonFormatProcessor();
}
}
再把FormatAutoConfiguration 通过@import导入
@Import(FormatAutoConfiguration.class)
@Configuration
public class HellAutoConfiguration {
@Bean
public HelloFormatTemplate helloFormatTemplate(FormatProcessor formatProcessor){
return new HelloFormatTemplate(formatProcessor);
}
}
在创建对外调用类
public class HelloFormatTemplate {
private FormatProcessor formatProcessor;
public HelloFormatTemplate(FormatProcessor formatProcessor ) {
this.formatProcessor=formatProcessor;
}
public <T> String doFormat(T obj){
StringBuilder stringBuilder= new StringBuilder();
stringBuilder.append("Execute format:").append("\n");
stringBuilder.append("Obj format result:").append(formatProcessor.format(obj)).append("\n");
return stringBuilder.toString();
}
}
基础工作就完成了项目结构如下图
然后创建resources/META-INF/spring.factories,在其中写下如下内容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.starter.autoconfiguration.HellAutoConfiguration
键说明是由哪个注解配置时进行扫描,值代表的注入bean.最后的项目结构如下图
将项目打包,通过新的项目pom文件导入,使用项目使用方法如下
@RestController
public class FormatController {
@Autowired
HelloFormatTemplate helloFormatTemplate;
@GetMapping("/format")
public String format(){
User user=new User();
user.setAge(16);
user.setName("杨某");
return helloFormatTemplate.doFormat(user);
}
}
由于在新项目中并没有com.alibaba.fast.json.JSON,所以会默认注入StringFormatProcessor。
写到这里基本内容已经实现,为了更加贴切,需要加上外置化配置,这里新添加一个类
@ConfigurationProperties(prefix = HelloProperties.HELLO_FORMAT_PROPERT)
public class HelloProperties {
public static final String HELLO_FORMAT_PROPERT="hello.format"; //配置文件中的键
private Map<String,Object> info;
public Map<String, Object> getInfo() {
return info;
}
public void setInfo(Map<String, Object> info) {
this.info = info;
}
}
再对已有的类进行改造
@Import(FormatAutoConfiguration.class)
@EnableConfigurationProperties(HelloProperties.class)
@Configuration
public class HellAutoConfiguration {
@Bean
public HelloFormatTemplate helloFormatTemplate(FormatProcessor formatProcessor,HelloProperties helloProperties){
return new HelloFormatTemplate(formatProcessor,helloProperties);
}
}
//-------分隔符------
public class HelloFormatTemplate {
private FormatProcessor formatProcessor;
private HelloProperties helloProperties;
public HelloFormatTemplate(FormatProcessor formatProcessor,HelloProperties helloProperties ) {
this.formatProcessor=formatProcessor;
this.helloProperties=helloProperties;
}
public <T> String doFormat(T obj){
StringBuilder stringBuilder= new StringBuilder();
stringBuilder.append("Execute format:").append("<br/>");
stringBuilder.append("hellProperties:").append(formatProcessor.format(helloProperties.getInfo())).append("<br/>");
stringBuilder.append("Obj format result:").append(formatProcessor.format(obj)).append("<br/>");
return stringBuilder.toString();
}
}
这里补充下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.starter</groupId>
<artifactId>format-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<name>format-spring-boot-starter</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.6.RELEASE</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
打包后,就跟前面的方式一样使用,可以在另一个项目的application.properties写入内容
hello.format.info.country=CN
hello.format.info.provice=BeiJin
效果如下
以上内容基于SpringBoot核心配置构建,具体缘由可以看源码或者我的另一篇文章。