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