springboot 自定义starter
文章目錄
- 一、starter啟動(dòng)原理
- 二、自定義starter
- 1、say-spring-boot-starter-autoconfigure(自動(dòng)配置包)
- ①、項(xiàng)目結(jié)構(gòu)
- ②、pom文件
- ③、實(shí)體類 DialogBean
- ④、業(yè)務(wù)類 SayService
- ⑤、自動(dòng)配置類 SayServiceAutoConfiguration
- ⑥、配置使用 META-INF/spring.factories
- 2、say-spring-boot-starter(啟動(dòng)器)
- ①、項(xiàng)目結(jié)構(gòu)
- ②、pom 文件
- 三、測(cè)試自定義 starter
- 1、項(xiàng)目結(jié)構(gòu)
- 2、 pom 文件
- 3、application.yml 配置
- 4、 controller 測(cè)試
- 5、自定義 Bean
一、starter啟動(dòng)原理
-
starter-pom引入 autoconfigurer 包
-
autoconfigure包中配置使用 META-INF/spring.factories 中 EnableAutoConfiguration 的值,使得項(xiàng)目啟動(dòng)加載指定的自動(dòng)配置類
-
編寫自動(dòng)配置類 xxxAutoConfiguration -> xxxxProperties
- @Configuration
- @Conditional
- @EnableConfigurationProperties
- @Bean
…
引入starter — xxxAutoConfiguration — 容器中放入組件 ---- 綁定xxxProperties ---- 配置項(xiàng)
二、自定義starter
1、say-spring-boot-starter-autoconfigure(自動(dòng)配置包)
創(chuàng)建一個(gè) springboot 項(xiàng)目 say-spring-boot-starter-autoconfigure
①、項(xiàng)目結(jié)構(gòu)
②、pom文件
我就引入了一個(gè) lombok ,簡(jiǎn)寫實(shí)體類,spring-boot-starter 是 springboot 自帶的
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies>③、實(shí)體類 DialogBean
使用 @ConfigurationProperties(prefix = “ye.dialog”) 綁定 application.yml 中的 ye.dialog 配置
@Data @ConfigurationProperties(prefix = "ye.dialog") public class DialogBean {private String name;private String words; }④、業(yè)務(wù)類 SayService
這塊不需要使用 @Service 加入容器,通過(guò)后面的自動(dòng)配置類注入容器
public class SayService {private boolean flag;@AutowiredDialogBean dialogBean;public String say() {if (flag) {return dialogBean.getName() + ":姨給你來(lái)個(gè)唱跳rap,你看滿意不";}return dialogBean.getName() + ":" + dialogBean.getWords();}public void setFlag(boolean flag) {this.flag = flag;} }⑤、自動(dòng)配置類 SayServiceAutoConfiguration
如果你有多個(gè)自定義組件,可以注入多個(gè) Bean
- @Configuration 表明配置類
- @Bean 注冊(cè)組件 SayService
- @EnableConfigurationProperties(DialogBean.class)
- EnableConfigurationProperties 讓 @ConfigurationProperties 注解生效
- @EnableConfigurationProperties + @ConfigurationProperties(在DialogBean 實(shí)體類使用) 注冊(cè)組件,這樣步驟 ④ 的 @Autowired 就可以拿到 DialogBean 組件
- @ConditionalOnMissingBean(SayService.class),如果 DialogBean 組件不在容器中則執(zhí)行下面方法
⑥、配置使用 META-INF/spring.factories
在根目錄 resources下新建文件 spring.factories 文件,查看 springboot 的 factories 文件,仿照配置如下,其中 \ 代表?yè)Q行,寫在一行也可以,不美觀
# Auto Configure # 必要,寫死的 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ # 你的配置類 com.ye.autoconfig.SayServiceAutoConfiguration最后 clean,install 安裝到 maven 倉(cāng)庫(kù)
2、say-spring-boot-starter(啟動(dòng)器)
創(chuàng)建一個(gè)maven項(xiàng)目 say-spring-boot-starter,引入 say-spring-boot-starter-autoconfigure 自動(dòng)配置
①、項(xiàng)目結(jié)構(gòu)
②、pom 文件
只需要導(dǎo)入自動(dòng)配置依賴即可,該項(xiàng)目也可以定義一些功能,可自行實(shí)現(xiàn)
<dependency><groupId>com.ye</groupId><artifactId>say-spring-boot-starter-autoconfigure</artifactId><version>1.0.0</version></dependency>clean, install 安裝到 maven 倉(cāng)庫(kù)
三、測(cè)試自定義 starter
新建一個(gè) springboot 項(xiàng)目,用于測(cè)試上面自定義的 starter
1、項(xiàng)目結(jié)構(gòu)
2、 pom 文件
注意導(dǎo)入你自己的依賴,版本信息
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.ye</groupId><artifactId>say-spring-boot-starter</artifactId><version>1.0.0</version></dependency>3、application.yml 配置
ye:dialog:name: 張姨words: 來(lái),姨給你設(shè)個(gè)話4、 controller 測(cè)試
@RestController public class SayHello {@AutowiredSayService sayService;@GetMapping(value = "/say")public String say(){return sayService.say();} }自動(dòng)裝配正常
5、自定義 Bean
自定義 Bean,修改默認(rèn)配置,個(gè)性化設(shè)置
@Configuration public class myConfig {@BeanSayService mySayService(){SayService sayService = new SayService();sayService.setFlag(true);return sayService;} }個(gè)性化定制成功
如上就是 springboot 自定義starter 的全部過(guò)程,項(xiàng)目比較簡(jiǎn)單,適合于新手入門,大佬輕噴
總結(jié)
以上是生活随笔為你收集整理的springboot 自定义starter的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 我心中的计算机作文500,我眼中的作文5
- 下一篇: 基于深度学习的目标检测算法思维导图