Spring?Boot自定義Starter組件開發(fā)實現(xiàn)配置過程
自定義starter
SpringBoot中的starter是一種非常重要的機制,能夠拋棄以前繁雜的配置,將其統(tǒng)一集成進 starter,應用者只需要在maven中引入starter依賴,SpringBoot就能自動掃描到要加載的信息并啟 動相應的默認配置。starter讓我們擺脫了各種依賴庫的處理,需要配置各種信息的困擾。 SpringBoot會自動通過classpath路徑下的類發(fā)現(xiàn)需要的Bean,并注冊進IOC容器。SpringBoot提供 了針對日常企業(yè)應用研發(fā)各種場景的spring-boot-starter依賴模塊。所有這些依賴模塊都遵循著約定 成俗的默認配置,并允許我們調整這些配置,即遵循“約定大于配置”的理念。
為什么要自定義starter
在我們的日常開發(fā)工作中,經常會有一些獨立于業(yè)務之外的配置模塊,我們經常將其放到一個特定的 包下,然后如果另一個工程需要復用這塊功能的時候,需要將代碼硬拷貝到另一個工程,重新集成一 遍,麻煩至極。如果我們將這些可獨立于業(yè)務代碼之外的功能配置模塊封裝成一個個starter,復用的時 候只需要將其在pom中引用依賴即可,SpringBoot為我們完成自動裝配,簡直不要太爽。
自定義starter的命名規(guī)則
SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建議自定義的starter使用 xxx-spring-boot-starter命名規(guī)則。以區(qū)分SpringBoot生態(tài)提供的starter。
有了以上的了解后,來創(chuàng)建 Maven 項目,目錄結構如下:
實現(xiàn)方法
實現(xiàn)自定義starter大致分一下幾步:
1.引入pom依賴
2.編寫測試用例類
3.創(chuàng)建自動配置類
4.在resources包下增加配置文件
引入依賴
<?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>cn.mystylefree</groupId> <artifactId>custom-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.7.0</version> </dependency> </dependencies> </project>
這里引入了自動配置類
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.7.0</version> </dependency> </dependencies>
編寫測試類
在使用Spring官方的Starter時通??梢栽赼pplication.properties中來配置參數(shù)覆蓋掉默認的值。即name的值會被配置文件中的值替換掉。
@EnableConfigurationProperties({PersonProperties.class}) //開啟ConfigurationProperties注解 @ConfigurationProperties(prefix = "person") public class PersonProperties { private int id; private String name="章三"; }
省略set/get方法和toString方法
創(chuàng)建配置類
@Configuration //當類路徑classpath下有指定當類 (SimpleBean) 的情況下進行自動配置 @ConditionalOnClass(PersonProperties.class) public class MyAutoConfiguration { static { System.out.println("MyAutoConfiguration init ..."); } @Bean public PersonProperties personProperties(){ return new PersonProperties(); } }
創(chuàng)建spring.factories文件
/META-INF/spring.factories文件放在/src/main/resources目錄下
注意:META-INF是自己手動創(chuàng)建的目錄,spring.factories也是自己手動創(chuàng)建的文件,在該文件中配置自己的自動配置類。
一定要按照下面的位置結構添加
文件中的內容如下
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ cn.mystylefree.config.MyAutoConfiguration
這時配置都加好了
只需從新打下包就行
mvn clean install
我們自己設置的starter包就保存到本地的maven倉庫了
接下來我們就能使用剛剛自定義的jar包實現(xiàn)pom依賴引用了
這是我的maven依賴地址
將這個依賴加入到新的項目中引入即可
這是我的一個新springboot項目
1.加入依賴
根據自己的名稱添加
<dependency> <groupId>cn.mystylefree</groupId> <artifactId>custom-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
2.設置application.properties
person.id=1 person.name=小米
由于設置的前綴名上person
3.添加測試用例
@SpringBootTest class SpringBootDemoApplicationTests { @Autowired private PersonProperties personProperties; @Test void contextLoads() { int id = personProperties.getId(); String name = personProperties.getName(); System.out.println(id+name); } }
這時 調試就能使用自定義的starter了
但是發(fā)現(xiàn)出現(xiàn)中文亂嗎了,不慌設置一下編碼格式
重新啟動項目
亂碼問題
發(fā)現(xiàn)還是亂碼???
在Spring Boot項目中,有時候需要自定義一些配置,如果使用中文值并使用注解讀取時就會出現(xiàn)亂碼。
原因: Spring Boot注解讀取application.properties或者application-{profile}.properties文件時默認的是ISO_8859_1編碼。
解決方案:
1. 使用yml配置文件進行配置。
Spring Boot在讀取yaml配置文件時使用的是UTF-8的編碼方式。
2. 使用自定義配置文件如:
custom.properties配置中文屬性,并使用@PropertySource(value="classpath:custom.properties", encoding="UTF-8")注解指定讀取的文件和編碼。代碼如下:
@Data @ConfigurationProperties(prefix = "custom.user") @PropertySource(value="classpath:custom.properties", encoding="UTF-8") public class UserProperties { // @Value("${custom.user.name}") private String name; // @Value("${custom.user.sex}") private String sex; }
使用@ConfigurationProperties和@Value均可以正常讀取。
3. 把中文換成對應的ASCII碼。
custom.user.sex=男
換成:
custom.user.sex=\u7537
以上三種方法均可以正常讀取配置文件中的中文字符。
參考文檔:
Spring Boot使用@ConfigurationProperties或者@Value讀取properties文件中文亂碼
到此這篇關于SpringBoot自定義Starter組件開發(fā)實現(xiàn)的文章就介紹到這了,更多相關SpringBoot自定義Starter組件開發(fā)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Boot中自定義注解結合AOP實現(xiàn)主備庫切換問題
這篇文章主要介紹了Spring Boot中自定義注解+AOP實現(xiàn)主備庫切換的相關知識,本篇文章的場景是做調度中心和監(jiān)控中心時的需求,后端使用TDDL實現(xiàn)分表分庫,需要的朋友可以參考下2019-08-08Java list與set中contains()方法效率案例詳解
這篇文章主要介紹了Java list與set中contains()方法效率案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08SpringBoot使用EmbeddedDatabaseBuilder進行數(shù)據庫集成測試
在開發(fā)SpringBoot應用程序時,我們通常需要與數(shù)據庫進行交互,為了確保我們的應用程序在生產環(huán)境中可以正常工作,我們需要進行數(shù)據庫集成測試,在本文中,我們將介紹如何使用 SpringBoot 中的 EmbeddedDatabaseBuilder 來進行數(shù)據庫集成測試2023-07-07Spring?Boot中的@EnableAutoConfiguration注解詳解
這篇文章主要介紹了Spring?Boot中的@EnableAutoConfiguration注解詳解,Spring?Boot是一個非常流行的Java框架,它可以快速創(chuàng)建基于Spring的應用程序。Spring?Boot提供了許多自動配置功能,使得開發(fā)者可以非常容易地創(chuàng)建一個可運行的應用程序,需要的朋友可以參考下2023-08-08