SpringBoot之如何自定義一個(gè)starter模塊
前言
前面我們說到了在SpringBoot中自定義Enable模塊,那么如何在SpringBoot中如何自定義一個(gè)Starter模塊呢?要想自定義一個(gè)Starter模塊,就需要理解自動(dòng)裝配的原理。
首先,想想我們自動(dòng)裝配涉及到哪些關(guān)鍵的類或者屬性文件?無非就是配置類+spring.factories屬性文件。
自定義starter的條件
根據(jù)條件檢查classpath下對(duì)應(yīng)的類,也就是說需要提供對(duì)應(yīng)可檢查的類。
當(dāng)滿足條件時(shí)能夠生成自定義的Bean,并注冊(cè)到容器中去。
能夠自動(dòng)配置項(xiàng)目所需要的配置。
開始實(shí)施
第一步:定義好需要通過配置類來實(shí)例化的Bean
public class MsgService {
private String url;
private String content;
public MsgService(String url, String content) {
this.url = url;
this.content = content;
}
public String sendMsg() {
System.out.println("**********消息發(fā)送成功,地址=" + url + ",內(nèi)容=" + content + "");
return "消息發(fā)送成功,地址=" + url + ",內(nèi)容=" + content + "";
}
}
這里定義的bean沒啥好說的。
第二步: 定義好屬性類
@ConfigurationProperties(prefix = “msg”)
public class MsgProperties {
/**
* 消息發(fā)送地址
/
private String url;
/*
* 發(fā)送內(nèi)容
*/
private String content;
//get,set方法省略
這里通過@ConfigurationProperties注解將配置文件的前綴為msg的配置信息與自身的屬性綁定,所有在配置文件中能配置的屬性都在MsgProperties類中封裝著,配置文件能配置什么只需要看這個(gè)屬性類的屬性。
第二步:定義好配置類
@Configuration
@ConditionalOnClass(MsgService.class)
@EnableConfigurationProperties(MsgProperties.class)
public class MsgConfiguration {
/**
* 注入屬性類
*/
@Autowired
private MsgProperties msgProperties;
@Bean
@ConditionalOnMissingBean({MsgService.class})
public MsgService msgService() {
return new MsgService(msgProperties.getUrl(), msgProperties.getContent());
}
}
@Bean注解表明該方法實(shí)例化的對(duì)象會(huì)被加載到容器當(dāng)中;
@ConditionalOnMissingBean注解指明當(dāng)容器中不存在MsgService的對(duì)象時(shí)再進(jìn)行實(shí)例化;@EnableConfigurationProperties注解是使MsgProperties生效,也就是將MsgProperties類注入到IOC容器中。
@ConditionalOnClass 注解表明只有classpath下能找到MsgService類時(shí)才會(huì)構(gòu)建這個(gè)Bean。
第三步:定義好spring.factories屬性文件
要想實(shí)現(xiàn)自動(dòng)配置,那么spring.factories屬性文件是必不可少的,因?yàn)镾pringBoot需要通過spring.factories找到需要實(shí)例化的配置類。然后通過SPI的方式來實(shí)例化。
所以,我們需要在resources 下面增加 META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.jay.config.MsgConfiguration
打包上傳到本地倉庫
上面的步驟都搞好之后,我們這個(gè)自定義的starter模塊差不多就可以用了,為了上其他項(xiàng)目可以引入我們的自定義的starter模塊,我們需要通過mvn install命令將這個(gè)starter包上傳到我們本地倉庫或者私服。
其他項(xiàng)目引入我們自定義的starter模塊
- 引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!--自定義的starter-->
<dependency>
<groupId>com.jay</groupId>
<artifactId>springboot-custom-starter2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
- 在application.properties文件中增加配置
msg.url=127.0.0.1
msg.content=nice to meet you
3.測試
@RestController
public class HelloWorldController {
@Autowired
private MsgService msgService;
@RequestMapping(value = "/testSendMsg")
public String testSendMsg() {
String sendMsg = msgService.sendMsg();
return sendMsg;
}
}
啟動(dòng)項(xiàng)目,訪問接口,結(jié)果如下:
總結(jié)
本文完整的演示了一遍如何自定義starter模塊。其實(shí)理解了自動(dòng)裝配的原理就很好的能自定義一個(gè)starter模塊。它的工作流程無非就三步:
Spring Boot在啟動(dòng)時(shí)會(huì)掃描項(xiàng)目所依賴的JAR包,尋找包含spring.factories屬性文件的JAR包。
根據(jù)spring.factories配置加載EnableAutoConfiguration。
根據(jù)@Conditional注解的條件,進(jìn)行自動(dòng)配置并將Bean注入到Spring容器。
作者:碼農(nóng)飛哥
微信公眾號(hào):碼農(nóng)飛哥