實現一個簡單的對象格式化starter
新建一個maven工程,引入spring-boot-starter和json
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.david</groupId>
<artifactId>my-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<!--可選-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
<optional>true</optional>
</dependency>
<!-- spring默認支持yaml,如果需要支持配置文件格式properties或xml需要添加這個依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</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>
定義一個接口類IFormat,並實現2個不同的類,一個字符串一個json
public interface IFormat {
<T> String doFormat(T obj);
}
實現類
json實現類
import com.alibaba.fastjson.JSON;
public class JsonFormat implements IFormat {
@Override
public <T> String doFormat(T obj) {
return "Json Format:"+ JSON.toJSONString(obj);
}
}
字符串實現類
public class StringFormat implements IFormat {
@Override
public <T> String doFormat(T obj) {
return "String Format:"+obj.toString();
}
}
創建spring配置類
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class FormatAutoConfiguration{
@ConditionalOnMissingClass("com.alibaba.fastjson.JSON")// 當沒有json這個類時會加載這個bean,並交給spring容器管理
@Primary // 設置一個首選項,不然會提示找到2個bean
@Bean
public IFormat stringFormat(){
return new StringFormat();
}
@ConditionalOnClass(name = "com.alibaba.fastjson.JSON")// 當有json這個類時會加載這個bean,並交給spring容器管理
@Bean
public IFormat jsonFormat(){
return new JsonFormat();
}
}
創建springboot中可以在properties自動提示的類,這樣就可以動態的配置一些屬性,這裏只定義了一個屬性
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "com.david.format")// 自動提示的配置前綴
public class FormatProperties {
private String info;
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
接着定義裝配的模板
public class FormatTemplate {
private IFormat format;
private FormatProperties formatProperties;
public FormatTemplate(IFormat format, FormatProperties formatProperties) {
this.format = format;
this.formatProperties = formatProperties;
}
public <T> String doFormat(T obj){
return "properties info:"+formatProperties.getInfo()+";"+format.doFormat(obj);
}
}
然後定義自動裝配模板的配置類,spring會自動注入兩個bean創建FormatTemplate並交由spring容器管理
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Import(FormatAutoConfiguration.class)// 導入其他bean的配置
@EnableConfigurationProperties(FormatProperties.class)// 啓用配置文件
@Configuration
public class FormatTemplateAutoConfiguration {
@Bean
public FormatTemplate formatTemplate(IFormat format, FormatProperties formatProperties){
return new FormatTemplate(format,formatProperties);
}
}
最後一步就是整個自動裝配的核心了,在resources目錄下創建一個META-INF文件夾,然後在META-INF文件夾下創建一個spring.factories文件,文件內容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.david.FormatTemplateAutoConfiguration
springboot預留了擴展點(SPI),通過在資源路徑下創建META-INF文件夾,在META-INF下創建spring.factories文件,spring啓動時會掃描所有資源文件下的這個文件,並根據文件配置內容加載對應bean的信息交由spring管理,這樣我們就能使用autowired的註解實現快速注入使用,starter就是根據這個原理實現的。
項目結構如下圖
接着把這個項目install成jar,在創建一個新的springboot工程引用這個jar
新工程pom.xml,目前還沒有依賴json的jar包
<?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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.david</groupId>
<artifactId>my-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>my-web</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.david.my</groupId>
<artifactId>mycode</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.david</groupId>
<artifactId>my-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
創建一個user類,user類裏面有name和age屬性並重寫toString方法
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
創建一個controller並自動注入FormatTemplate
import com.david.FormatTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Autowired
FormatTemplate formatTemplate;
@RequestMapping("/format")
public String format(){
User user = new User();
user.setName("David");
user.setAge(27);
return formatTemplate.doFormat(user);
}
}
在application.properties中輸入會有自動提示
啓動運行項目,目前輸出會是string格式化對象,配置的屬性也有輸出
加上json的jar的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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.david</groupId>
<artifactId>my-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>my-web</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.david.my</groupId>
<artifactId>mycode</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.david</groupId>
<artifactId>my-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- json jar 包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
重啓並訪問