SpringBoot中的自定義starter
自定義starter
所謂的 starter ,其實就是一個普通的 Maven 項目。
1、首先創(chuàng)建一個普通的 Maven 項目
引入自動化配置依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.8.RELEASE</version>
<!-- 表明不傳遞spring-boot-autoconfigure依賴 -->
<optional>true</optional>
</dependency>
注意:在自定義的starter 的pom中,將spring-boot-autoconfigure的maven依賴聲明為<optional>true</optional>,表明自定義的starter的jar包不會傳遞spring-boot-autoconfigure依賴;否則會將spring-boot-autoconfigure版本固定,導致引用自定義starter的應用出現(xiàn)版本沖突問題。
starter命名模式 --> ${module}-spring-boot-starter;
例如:
<groupId>com.hello</groupId> <artifactId>helloService-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version>
2、創(chuàng)建一個配置類
創(chuàng)建一個配置類 HelloProperties 來接受 application.properties 中注入的值:
@ConfigurationProperties(prefix = "javaboy")
public class HelloProperties {
privatestaticfinal String DEFAULT_NAME = "默認名";
privatestaticfinal String DEFAULT_MSG = "默認消息";
private String name = DEFAULT_NAME;
private String msg = DEFAULT_MSG;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}3、創(chuàng)建一個服務類 HelloService
public class HelloService {
private String msg;
private String name;
public String sayHello() {
return name + " say " + msg + " !";
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}4、自定義自動配置類
SpringBoot自動裝配的命名規(guī)則:
- 自動裝配類應命名為:XxxAutoConfiguration;
- 自動裝配package命名模式: ${root-package}.autoconfigure.${module-package},比如:
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setName(helloProperties.getName());
helloService.setMsg(helloProperties.getMsg());
return helloService;
}
}注解 @EnableConfigurationProperties 使我們之前配置的 @ConfigurationProperties 生效,讓配置的屬性注入到 Bean 中。
注解 @ConditionalOnClass 表示當項目的 classpath 下存在 HelloService 時,后面的配置才生效。
5、創(chuàng)建 spring.factories 文件
在resources目錄下新建 META-INF/spring.factories 文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration = \ com.hello.autoconfigure.HelloServiceAutoConfiguration

這里的key是固定的,value是我們的自動配置類的全路徑。
SpringBoot 項目啟動時,會做自動配置,會去掃描所有jar包中的 META-INF/spring.factories 配置文件。
至此,自定義的starter已完成。將這個自動化配置類的項目安裝到maven倉庫中,然后在其他項目中引入依賴就可以使用。
新建一個普通的 Spring Boot 工程,并加入我們自定義的 starter 的依賴:
<dependency> <groupId>com.hello</groupId> <artifactId>helloService-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
此時我們項目中已有一個 helloService 實例可以使用,而且我們還可以配置它的屬性數(shù)據(jù),例如,在 application.properties 中配置:
javaboy.name=我的名字 javaboy.msg=提示消息
我們做個單元測試:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MystarterApplicationTest {
@Autowired
private HelloService helloService;
@Test
public void contextLoads() {
System.out.println(helloService.sayHello());
}
}控制臺會打印:
我的名字 say 提示消息 !
到此這篇關(guān)于SpringBoot中的自定義starter的文章就介紹到這了,更多相關(guān)自定義starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Jackson實現(xiàn)API接口數(shù)據(jù)脫敏的示例詳解
用戶的一些敏感數(shù)據(jù),例如手機號、郵箱、身份證等信息,在數(shù)據(jù)庫以明文存儲,但在接口返回數(shù)據(jù)給瀏覽器(或三方客戶端)時,希望對這些敏感數(shù)據(jù)進行脫敏,所以本文就給大家介紹以惡如何利用Jackson實現(xiàn)API接口數(shù)據(jù)脫敏,需要的朋友可以參考下2023-08-08
mybatis-plus 關(guān)于savebatch,saveorupdatebatch遇到的坑及解決辦法
本文主要介紹了mybatis-plus 關(guān)于savebatch,saveorupdatebatch遇到的坑及解決辦法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01
Java中HttpServletRequestWrapper的使用與原理詳解
這篇文章主要介紹了Java中HttpServletRequestWrapper的使用與原理詳解,HttpServletRequestWrapper 實現(xiàn)了 HttpServletRequest 接口,可以讓開發(fā)人員很方便的改造發(fā)送給 Servlet 的請求,需要的朋友可以參考下2024-01-01

