前言
SpringBoot幾乎是我們所有Java開發者必須掌握的一個技能,它為所有開發者更快的入門,做到開箱即用,沒有冗余的代碼和XML配置要求,對于開發者來說幾乎是“零配置”,這個得益于SpringBoot的“約定大于配置”,
SpringBoot的starter幫我們把繁瑣的配置和注冊到IOC容器的程序都做了,我們只需要按照約定配置就可以開箱即用,實作零配置,下面我們就手擼一個spring-boot-starter來加深對“零配置”和“約定大于配置”的理解吧,
spring-boot-starter介紹
SpringBoot提供了一個方便我們開發者開發的環境,它的底層就是使用了starter機制,能夠拋棄以前繁瑣的配置,統一集成為starter,
開發者只需要把starter引入到你的pom.xml中,SpringBoot就會自動掃描要加載的資訊并啟動相應的默認配置,SpringBoot會自動通過classpath路徑下的類發現需要的Bean,并注冊到IOC容器中,
所有這些依賴的模塊都遵循著約定成俗的默認配置,并允許我們修改配置資訊,這就是SpringBoot的“約定大于配置”的理念,
手擼xxx-spring-boot-starter
新建一個json-template-spring-boot-starter的SpringBoot專案,并引入fastjson依賴,

接著創建一個JsonTemplate,呼叫fastjson把一個物件轉換為json字串
package god.jiang.jsontemplatespringbootstarter;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
@Data
public class JsonTemplate {
private String name;
private String text;
public String objToJsonString(Object object) {
return this.getName() + this.getText() + JSONObject.toJSONString(object);
}
}
接著創建spring-boot-starter的配置類(約定大于配置)
package god.jiang.jsontemplatespringbootstarter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "god.jiang.json")
public class JsonProperties {
public String name = "god-jiang-json";
public String text = "自定義spring-boot-starter把物件轉換成json";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
接著創建自動配置類,讓springboot自動掃描并且注入到IOC容器中
package god.jiang.jsontemplatespringbootstarter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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;
@Configuration
@ConditionalOnClass({JsonTemplate.class})
@EnableConfigurationProperties(JsonProperties.class)
public class JsonTemplateAutoConfiguration {
@Autowired
private JsonProperties jsonProperties;
@Bean
@ConditionalOnMissingBean(JsonTemplate.class)
public JsonTemplate jsonTemplate(){
JsonTemplate jsonTemplate = new JsonTemplate();
jsonTemplate.setName(jsonProperties.getName());
jsonTemplate.setText(jsonProperties.getText());
return jsonTemplate;
}
}
上面做完了,最關鍵的一步把自動配置的類寫在resource/META-INF/spring.factories檔案中
org.springframework.boot.autoconfigure.EnableAutoConfiguration=god.jiang.jsontemplatespringbootstarter.JsonTemplateAutoConfiguration
然后用maven命令install和deploy到遠程倉庫,方便以后專案可以呼叫該starter,

使用我們上面所創建的spring-boot-starter
創建一個新的SpringBoot專案,在pom.xml中引入上面撰寫的json-templatespring-boot-starter

接著創建一個controller呼叫json-template-spring-boot-starter的objToJsonString方法
package god.jiang.teststarter;
import god.jiang.jsontemplatespringbootstarter.JsonTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@Autowired
private JsonTemplate jsonTemplate;
@RequestMapping("/toJson")
public String toJson(){
GodJiang godJiang = new GodJiang();
godJiang.setName("god-jiang");
godJiang.setAge(18);
godJiang.setText("自定義一個spring-boot-starter");
return jsonTemplate.objToString(godJiang);
}
}
按照約定配置application.properties
god.jiang.json.name=god-jiang
god.jiang.json.text=-study-spring-boot-starter
啟動專案運行訪問localhost:8080/toJson

總結
以上就是手寫一個spring-boot-starter的程序,實際上讀完springboot的自動加載原始碼我就大概知道整個加載的程序,這次接著手寫starter來實作整個程序,讓我對springboot原始碼有了更深的理解,希望以上的分享對你們有所幫助~~~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/255565.html
標籤:java
上一篇:redis五大資料結構和使用場景
下一篇:執行緒安全(三)Atomic是什么? Atomic如何保證原子性? Atomic比較案例 圖文解釋整個CAS程序 Atomic適用場景 Atomic缺點
