SpringBoot自定義starter方式
命名
推薦以xxx-spring-boot-starter命名
原理
引入spring-boot-starter-jdbc后可直接使用DataSource
1.加載自動配置類
通過SPI(Service Provider Interface,Java提供的服務(wù)發(fā)現(xiàn)機制,用于框架拓展和組件替換)原理
(1)@SpringBootApplication->@EnableAutoConfiguration->@Import
(2)@Import通過AutoConfigurationImportSelector::selectImports方法導(dǎo)入自動配置類
(3)在AutoConfigurationImportSelector::getCandidateConfigurations方法中得知導(dǎo)入的自動配置類在從META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件讀取
(4)其中包含DataSourceAutoConfiguration類
2.通過xxxAutoConfiguration再導(dǎo)入功能所需的bean
(1)
DataSourceAutoConfiguration->@EnableConfigurationProperties(DataSourceProperties.class)
將Properties類生效,讀取application.yml中數(shù)據(jù)(url、username、password...)
(2)
@Import({ DataSourceConfiguration.Hikari.class... })
導(dǎo)入各種連接池,根據(jù)條件注解生效不同連接池
自定義starter
示例:@Log的添加日志功能封裝成starter
1.創(chuàng)建標(biāo)準(zhǔn)SpringBoot項目引入所需依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
2.編寫Properties類
@Data @Component @ConfigurationProperties("log") public class LogProperties{ private Boolean classFullName= true; //日志展示全類名 private Boolean showUseTime= true; //日志展示方法執(zhí)行時間 }
3.編寫application.yml配置文件
log: class-full-name: true #展示全類名 show-use-time: true #展示方法執(zhí)行時間
4.編寫@Log注解
@Target(METHOD) //該注解只能用于方法上 @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface Log{}
5.編寫AOP實現(xiàn)具體功能
@Slf4j @Aspect @Component public class LogAop { @Resource private LogProperties logProperties; @Around("@annotation(com.gok.log.annotation.Log)") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { long beginTime = System.currentTimeMillis(); Class targetClass = joinPoint.getSignature().getDeclaringType(); String functionName = joinPoint.getSignature().getName(); String name = (logProperties.getClassFullName() ? targetClass.getName() : targetClass.getSimpleName()) + "#" + functionName; try { log.info("{}開始", name); Object res = joinPoint.proceed(joinPoint.getArgs()); log.info("{}結(jié)束", name); return res; } catch (Throwable t) { log.info("{}異常," + t.getMessage(), name); throw t; } finally { if (logProperties.getShowUseTime()) { log.info("{}執(zhí)行時間:{}ms", name, System.currentTimeMillis() - beginTime); } } } }
SpringBoot默認(rèn)只掃描啟動類所在目錄,而Log實現(xiàn)所在的包不會掃描,有@Component也無效
所以通過原理中的SPI機制導(dǎo)入
6.刪除LogProperties類和LogAop類的@Component注解
統(tǒng)一在LogAutoConfiguration導(dǎo)入
@Configuration //引入Properties類 @EnableConfigurationProperties(LogProperties.class) //引入Aop @Import(LogAop.class) public class LogAutoConfiguration { }
7.編寫文件
編寫META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件
引入自動配置類com.gok.logstarter.config.LogAutoConfiguration
8.刪除SpringBoot啟動類(main方法所在類)、刪除pom.xml<build>標(biāo)簽
原因:作為starter不需要啟動類
9.補充
(1)自定義starter的application.yml可以刪除,最終生效的是引入starter項目中application.yml
(2)如果希望實現(xiàn)編寫application.yml文件智能提示,需要在自定義starter的pom.xml添加如下依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring aop 攔截業(yè)務(wù)方法,實現(xiàn)權(quán)限控制示例
這篇文章主要介紹了spring aop 攔截業(yè)務(wù)方法,實現(xiàn)權(quán)限控制示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01零基礎(chǔ)寫Java知乎爬蟲之獲取知乎編輯推薦內(nèi)容
上篇文章我們拿百度首頁做了個小測試,今天我們來個復(fù)雜的,直接抓取知乎編輯推薦的內(nèi)容,小伙伴們可算松了口氣,終于進入正題了,哈哈。2014-11-11java如何將Object數(shù)組轉(zhuǎn)換為指定類型數(shù)組
這篇文章主要介紹了java如何將Object數(shù)組轉(zhuǎn)換為指定類型數(shù)組,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08SpringBoot actuator 健康檢查不通過的解決方案
這篇文章主要介紹了SpringBoot actuator 健康檢查不通過的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07