SpringBoot自定義start詳細圖文教程
一、Mybatis 實現(xiàn) start 的原理
首先在寫一個自定義的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 官方的啟動器都是以 spring-boot-starter-命名的,代表了一個特定的應用類型。第三方的啟動器不能以 spring-boot開頭命名,它們都被 Spring Boot官方保留。一般第三方應該這樣命名,像 mybatis 的 mybatis-spring-boot-starter。
:::
【1】查看spring.factories
文件: 配置自動配置類MybatisAutoConfiguration
。spring.factories
會引導springboot
哪個是自動配置類。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
【2】進入MybatisAutoConfiguration.class
類:下面@EnableConfigurationProperties(MybatisProperties.class)
引入了配置文件MybatisProperties.class
。之后就可以利用這個配置文件里的參數(shù)實例化一個對象完成整個mybatis
的創(chuàng)建。
在 Spring開發(fā)過程中我們常使用到 @ConfigurationProperties注解,通常是用來將 properties和 yml配置文件屬性轉化為 Bean對象使用和修改。在獲取這些 Bean之前,首先需要使用 @EnableConfigurationProperties({ConfigBean.class}) 注解的作用是開啟 @ConfigurationProperties注解,當滿足 Condition 條件的時候才執(zhí)行。
@Configuration @ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) @ConditionalOnBean({DataSource.class}) /** * @ConfigurationProperties注解主要用來把properties配置文件轉化為bean來使用的, * 而@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。 * 如果只配置@ConfigurationProperties注解,在IOC容器中是獲取不到properties配置文件轉化的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】進入 MybatisProperties: 這里所有的屬性,就是之后我們在properties
配置文件中配置的項,而@ConfigurationProperties(prefix = "mybatis")
定義了前綴。舉個栗子:我們一般會在application.yml
或者application.properties
中xml
映射文件的路徑:mybatis.mapperLocations=classpath:mapping/*.xml
就是以mybatis
作為前綴的。
mybatis
正是這個MybatisProperties
的@ConfigurationProperties
配置的前綴而mapperLocations
就是我們這個MybatisProperties.class
的其中一個成員變量 !
@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)在來看最后一個問題:spring.factories
文件什么時候加載,我們定位到我們的啟動類,進入@SpringBootApplication
注解,點進去之后是一個@EnableAutoConfiguration
注解,再點進去可以看到一個叫做AutoConfigurationImportSelector.class
的類,就是這里了再點進去,在這個類的源碼里搜索spring.factories
原來springboot
會去META-INF
目錄下找到這個spring.factories
文件,到現(xiàn)在為止我們已經(jīng)理清楚了整個start
加載的流程:
【1】去META-INF
目錄下找到這個spring.factories
文件;
【2】通過文件內指定的類路徑,找到配置類;
【3】配置類加載進屬性類;
【4】配置類通過屬性類的參數(shù)構建一個新的Bean
;
二、用戶自定義 start
就按照這個Mybatis
的格式,自己寫一個redis
的start
由于spring.factories
是指定入口的我們可以放在最后寫。下面創(chuàng)建一個普通的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 //只有當Jedis 存在的時候 才執(zhí)行,就是說一定要引入了Jedis的依賴才會執(zhí)行這個配置 @ConditionalOnClass(Jedis.class) //引入屬性類 @EnableConfigurationProperties(RedisProperties.class) public class RedisAutoConfiguration { @Bean //當這個bean不存在的時候才執(zhí)行,防止重復加載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)建入口文件,編寫內容:指定配置文件的全路徑。隨后通過mvn install
打到本地倉庫。
org.springframework.boot.autoconfigure.EnableAutoConfiguration =com.yintong.myjedis.RedisAutoConfiguration
【4】測試: 然后我們新建一個springboot
項目,在pom
中加入依賴:
<dependency> <groupId>com.yintong</groupId> <artifactId>redis-start</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
【5】測試類: @Resource
的作用相當于@Autowired
,只不過@Autowired
按byType
自動注入,而@Resource
默認按byName
自動注入罷了。下面如果你能成功輸出就成功了!
@RunWith(SpringRunner.class) @SpringBootTest public class TestStartApplicationTests { @Resource private Jedis jedis; @Test public void contextLoads() { jedis.set("test","測試成功"); String test = jedis.get("test"); System.out.println(test); } }
總結
到此這篇關于SpringBoot自定義start的文章就介紹到這了,更多相關SpringBoot自定義start內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- spring boot 自定義starter的實現(xiàn)教程
- springboot自定義Starter的具體流程
- Spring boot創(chuàng)建自定義starter的完整步驟
- spring boot微服務自定義starter原理詳解
- springboot自定義starter實現(xiàn)過程圖解
- springboot自定義redis-starter的實現(xiàn)
- SpringBoot自動配置之自定義starter的實現(xiàn)代碼
- 使用SpringBoot自定義starter的完整步驟
- Java SpringBoot自定義starter詳解
- SpringBoot如何自定義starter
相關文章
JAVA中出現(xiàn)異常、拋出異常后續(xù)代碼是否執(zhí)行情況詳析
當產(chǎn)生異常后,并在異常處理器中進行執(zhí)行之后,程序會是如何的一種狀態(tài),是終止還是繼續(xù)執(zhí)行處理之后的代碼呢,下面這篇文章主要給大家介紹了關于JAVA中出現(xiàn)異常、拋出異常后續(xù)代碼是否執(zhí)行情況的相關資料,需要的朋友可以參考下2024-05-05MyBatis-Plus標簽@TableField之fill自動填充方式
這篇文章主要介紹了MyBatis-Plus標簽@TableField之fill自動填充方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06一文搞懂SpringMVC中@InitBinder注解的使用
@InitBinder方法可以注冊控制器特定的java.bean.PropertyEditor或Spring Converter和 Formatter組件。本文通過示例為大家詳細講講@InitBinder注解的使用,需要的可以參考一下2022-06-06