SpringBoot自定義start詳細(xì)圖文教程
一、Mybatis 實(shí)現(xiàn) start 的原理
首先在寫一個(gè)自定義的start
之前,我們先參考下Mybatis
是如何整合SpringBoot:mybatis-spring-boot-autoconfigure
依賴包:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency>
mybatis
依賴包展示: 重要文件:spring.factories
、MybatisAutoConfiguration
、MybatisProperties
:::tip
starters 命名:Spring Boot 官方的啟動(dòng)器都是以 spring-boot-starter-命名的,代表了一個(gè)特定的應(yīng)用類型。第三方的啟動(dòng)器不能以 spring-boot開(kāi)頭命名,它們都被 Spring Boot官方保留。一般第三方應(yīng)該這樣命名,像 mybatis 的 mybatis-spring-boot-starter。
:::
【1】查看spring.factories
文件: 配置自動(dòng)配置類MybatisAutoConfiguration
。spring.factories
會(huì)引導(dǎo)springboot
哪個(gè)是自動(dòng)配置類。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
【2】進(jìn)入MybatisAutoConfiguration.class
類:下面@EnableConfigurationProperties(MybatisProperties.class)
引入了配置文件MybatisProperties.class
。之后就可以利用這個(gè)配置文件里的參數(shù)實(shí)例化一個(gè)對(duì)象完成整個(gè)mybatis
的創(chuàng)建。
在 Spring開(kāi)發(fā)過(guò)程中我們常使用到 @ConfigurationProperties注解,通常是用來(lái)將 properties和 yml配置文件屬性轉(zhuǎn)化為 Bean對(duì)象使用和修改。在獲取這些 Bean之前,首先需要使用 @EnableConfigurationProperties({ConfigBean.class}) 注解的作用是開(kāi)啟 @ConfigurationProperties注解,當(dāng)滿足 Condition 條件的時(shí)候才執(zhí)行。
@Configuration @ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) @ConditionalOnBean({DataSource.class}) /** * @ConfigurationProperties注解主要用來(lái)把properties配置文件轉(zhuǎn)化為bean來(lái)使用的, * 而@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。 * 如果只配置@ConfigurationProperties注解,在IOC容器中是獲取不到properties配置文件轉(zhuǎn)化的bean的。 */ @EnableConfigurationProperties({MybatisProperties.class}) @AutoConfigureAfter({DataSourceAutoConfiguration.class}) public class MybatisAutoConfiguration { private static final Logger logger = LoggerFactory.getLogger(MybatisAutoConfiguration.class); private final MybatisProperties properties; private final Interceptor[] interceptors; private final ResourceLoader resourceLoader; private final DatabaseIdProvider databaseIdProvider; private final List<ConfigurationCustomizer> configurationCustomizers; public MybatisAutoConfiguration(MybatisProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider, ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider, ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) { this.properties = properties; this.interceptors = (Interceptor[])interceptorsProvider.getIfAvailable(); this.resourceLoader = resourceLoader; this.databaseIdProvider = (DatabaseIdProvider)databaseIdProvider.getIfAvailable(); this.configurationCustomizers = (List)configurationCustomizersProvider.getIfAvailable(); } //...... }
【3】進(jìn)入 MybatisProperties: 這里所有的屬性,就是之后我們?cè)?code>properties配置文件中配置的項(xiàng),而@ConfigurationProperties(prefix = "mybatis")
定義了前綴。舉個(gè)栗子:我們一般會(huì)在application.yml
或者application.properties
中xml
映射文件的路徑:mybatis.mapperLocations=classpath:mapping/*.xml
就是以mybatis
作為前綴的。
mybatis
正是這個(gè)MybatisProperties
的@ConfigurationProperties
配置的前綴而mapperLocations
就是我們這個(gè)MybatisProperties.class
的其中一個(gè)成員變量 !
@ConfigurationProperties( prefix = "mybatis" ) public class MybatisProperties { public static final String MYBATIS_PREFIX = "mybatis"; private String configLocation; private String[] mapperLocations; private String typeAliasesPackage; private String typeHandlersPackage; private boolean checkConfigLocation = false; private ExecutorType executorType; private Properties configurationProperties; @NestedConfigurationProperty private Configuration configuration; public MybatisProperties() { } //...... }
【4】現(xiàn)在來(lái)看最后一個(gè)問(wèn)題:spring.factories
文件什么時(shí)候加載,我們定位到我們的啟動(dòng)類,進(jìn)入@SpringBootApplication
注解,點(diǎn)進(jìn)去之后是一個(gè)@EnableAutoConfiguration
注解,再點(diǎn)進(jìn)去可以看到一個(gè)叫做AutoConfigurationImportSelector.class
的類,就是這里了再點(diǎn)進(jìn)去,在這個(gè)類的源碼里搜索spring.factories
原來(lái)springboot
會(huì)去META-INF
目錄下找到這個(gè)spring.factories
文件,到現(xiàn)在為止我們已經(jīng)理清楚了整個(gè)start
加載的流程:
【1】去META-INF
目錄下找到這個(gè)spring.factories
文件;
【2】通過(guò)文件內(nèi)指定的類路徑,找到配置類;
【3】配置類加載進(jìn)屬性類;
【4】配置類通過(guò)屬性類的參數(shù)構(gòu)建一個(gè)新的Bean
;
二、用戶自定義 start
就按照這個(gè)Mybatis
的格式,自己寫一個(gè)redis
的start
由于spring.factories
是指定入口的我們可以放在最后寫。下面創(chuàng)建一個(gè)普通的springboot
工程。
【1】編寫屬性類: 添加@ConfigurationProperties
注解和前綴redis
。之后我們就可以在properties
或yml
中 使用redis.port=
指定參數(shù)了;
@ConfigurationProperties(prefix = "redis") public class RedisProperties { private Integer port; private String host; private String password; private int index; //省略了get set 方法 }
【2】編寫配置類: 添加配置類注解 @Configuration 和加載條件,以及 @EnableConfigurationProperties(RedisProperties.class) 引入屬性類,注入到 IOC 容器中。
@Configuration //只有當(dāng)Jedis 存在的時(shí)候 才執(zhí)行,就是說(shuō)一定要引入了Jedis的依賴才會(huì)執(zhí)行這個(gè)配置 @ConditionalOnClass(Jedis.class) //引入屬性類 @EnableConfigurationProperties(RedisProperties.class) public class RedisAutoConfiguration { @Bean //當(dāng)這個(gè)bean不存在的時(shí)候才執(zhí)行,防止重復(fù)加載bean @ConditionalOnMissingBean public Jedis jedis(RedisProperties redisProperties) { Jedis jedis = new Jedis(redisProperties.getHost(), redisProperties.getPort()); jedis.auth(redisProperties.getPassword()); jedis.select(redisProperties.getIndex()); return jedis; } }
【3】編寫spring.factories
文件: 在resources
目錄下創(chuàng)建入口文件,編寫內(nèi)容:指定配置文件的全路徑。隨后通過(guò)mvn install
打到本地倉(cāng)庫(kù)。
org.springframework.boot.autoconfigure.EnableAutoConfiguration =com.yintong.myjedis.RedisAutoConfiguration
【4】測(cè)試: 然后我們新建一個(gè)springboot
項(xiàng)目,在pom
中加入依賴:
<dependency> <groupId>com.yintong</groupId> <artifactId>redis-start</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
【5】測(cè)試類: @Resource
的作用相當(dāng)于@Autowired
,只不過(guò)@Autowired
按byType
自動(dòng)注入,而@Resource
默認(rèn)按byName
自動(dòng)注入罷了。下面如果你能成功輸出就成功了!
@RunWith(SpringRunner.class) @SpringBootTest public class TestStartApplicationTests { @Resource private Jedis jedis; @Test public void contextLoads() { jedis.set("test","測(cè)試成功"); String test = jedis.get("test"); System.out.println(test); } }
總結(jié)
到此這篇關(guān)于SpringBoot自定義start的文章就介紹到這了,更多相關(guān)SpringBoot自定義start內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- spring boot 自定義starter的實(shí)現(xiàn)教程
- springboot自定義Starter的具體流程
- Spring boot創(chuàng)建自定義starter的完整步驟
- spring boot微服務(wù)自定義starter原理詳解
- springboot自定義starter實(shí)現(xiàn)過(guò)程圖解
- springboot自定義redis-starter的實(shí)現(xiàn)
- SpringBoot自動(dòng)配置之自定義starter的實(shí)現(xiàn)代碼
- 使用SpringBoot自定義starter的完整步驟
- Java SpringBoot自定義starter詳解
- SpringBoot如何自定義starter
相關(guān)文章
IDEA 錯(cuò)誤之找不到或無(wú)法加載主類的問(wèn)題
這篇文章主要介紹了IDEA 錯(cuò)誤之找不到或無(wú)法加載主類,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08JAVA中出現(xiàn)異常、拋出異常后續(xù)代碼是否執(zhí)行情況詳析
當(dāng)產(chǎn)生異常后,并在異常處理器中進(jìn)行執(zhí)行之后,程序會(huì)是如何的一種狀態(tài),是終止還是繼續(xù)執(zhí)行處理之后的代碼呢,下面這篇文章主要給大家介紹了關(guān)于JAVA中出現(xiàn)異常、拋出異常后續(xù)代碼是否執(zhí)行情況的相關(guān)資料,需要的朋友可以參考下2024-05-05MyBatis-Plus標(biāo)簽@TableField之fill自動(dòng)填充方式
這篇文章主要介紹了MyBatis-Plus標(biāo)簽@TableField之fill自動(dòng)填充方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06java 靜態(tài)代理 動(dòng)態(tài)代理深入學(xué)習(xí)
代理模式是常用的java設(shè)計(jì)模式,特征是代理類與委托類有同樣的接口,代理類主要負(fù)責(zé)為委托類預(yù)處理消息、過(guò)濾消息、把消息轉(zhuǎn)發(fā)給委托類,以及事后處理消息等,需要的朋友可以參考下2012-11-11使用Java DOM解析器修改XML文件內(nèi)容的操作方法
在Java中,XML文件的解析和修改可以通過(guò)多種方法實(shí)現(xiàn),其中DOM(Document Object Model)是一種常用的方式,在本文中,我們將介紹如何使用Java DOM解析器修改XML文件中的內(nèi)容,并給出一個(gè)具體的示例,需要的朋友可以參考下2024-08-08一文搞懂SpringMVC中@InitBinder注解的使用
@InitBinder方法可以注冊(cè)控制器特定的java.bean.PropertyEditor或Spring Converter和 Formatter組件。本文通過(guò)示例為大家詳細(xì)講講@InitBinder注解的使用,需要的可以參考一下2022-06-06