亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

SpringBoot如何實現starter原理詳解

瀏覽:2日期:2023-05-13 13:18:55

1、Mybatis 自定義配置的分析

在我們自定義starter之前我們寫了解一下Mybatis 是如何實現starter

在SpringBoot 引入的依賴如下:

<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency>

mybatis的maven 依賴,主要涉及到的內容,spring.factories、MybatisAutoConfiguration、MybatisProperties

SpringBoot如何實現starter原理詳解

我們來看一下 META-INF/spring.factories文件,這個文件是以Map 形式存放的。key是EnableAutoConfiguration類的全類名,

value是一個MybatisAutoConfiguration,這就是當項目啟動自動配置的類。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration

MybatisAutoConfiguration

SpringBoot如何實現starter原理詳解

@Configuration //標示是一個配置類

@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) //表示當SqlSessionFactory,SqlSessionFactoryBean存在這個配置類才生效。

@EnableConfigurationProperties({MybatisProperties.class}):就是把 MybatisProperties加入到 IOC 容器中。

MybatisProperties

SpringBoot如何實現starter原理詳解

對于@ConfigurationProperties注解它的作用就是把全局配置文件中的值綁定到實體類JavaBean上面(將配置文件中的值與MybatisProperties綁定起來),而@EnableConfigurationProperties主要是把以綁定值JavaBean加入到spring容器中。

分析完這些規則后,我們再來看看mybatis自定義的starter 的項目結構,主要是分為兩個項目(一個是空項目(mtbatis-spring-boot-starter),一個是具體的實現自定義配置的項目(mybatis-spring-boot-autoconfigure)),空項目只是引入自定義配置項目的依賴,而實現映入的時候我們只需要映入空項(mtbatis-spring-boot-starter)即可。

到此我們已經分析完mybatis 自定義的starter,下面我們自己來實現一個自定義的starter。

2、自定義starter的實現

項目結構展示:

SpringBoot如何實現starter原理詳解

首先我們先定義一個 zfauto-spring-boot-autoconfigure 工程

編寫屬性類:添加 @ConfigurationProperties注解和前綴 zf.auto。之后我們就可以在 application.properties或application.yml 中 使用 zf.auto=指定參數了,由于篇幅的原因省略setter getter方法,實際是需要的,不然無法注入;

@ConfigurationProperties(prefix = 'zf.auto')public class HelloProperties { private String prefix; private String suffix;}

編寫配置類:加入@Configuration注解,@ConditionalOnWebApplication是web 應用配置類才起作用,以及 @EnableConfigurationProperties(HelloProperties.class) 注解,將屬性注入到 IOC 容器中。

@Configuration@ConditionalOnWebApplication@EnableConfigurationProperties(HelloProperties.class)public class HelloServiceAutoConfiguration { @Autowired HelloProperties helloProperties; @Bean public HelloService helloService(){ HelloService helloService=new HelloService(); helloService.setHelloProperties(helloProperties); return helloService; }}

編寫 spring.factories 文件:在resources路徑下面創建META-INF,文件夾,然后創建spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zfauto.starter.HelloServiceAutoConfiguration

然后我們在創建一個空項目(zfauto-spring-boot-starter),在這個項目中我們引入zfauto-spring-boot-autoconfigure依賴

<dependency> <groupId>com.zfauto.starter</groupId> <artifactId>zfauto-spring-boot-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version></dependency>

HelloService 實現的功能,省略setter,getter的方法(實際需要)

public class HelloService { HelloProperties helloProperties; public String sayHello(String name){ return helloProperties.getPrefix()+ ','+name+','+helloProperties.getSuffix(); }}

最后我們 分別將項目打包,由于zfauto-spring-boot-starter是依賴于zfauto-spring-boot-autoconfigure,所以我們先對zfauto-spring-boot-autoconfigure進行打包,然后通過 mvn install 打到本地倉庫(如何打包見下圖)。

SpringBoot如何實現starter原理詳解

到此我們自定義的類實現。那我們來測試一下,這個和我們引入其他的starter一樣了。

創建項目zfauto-spring-boot-starter-test ,引入自定義starter的依賴。

<dependency> <groupId>com.zfauto.starter</groupId> <artifactId>zfauto-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version></dependency>

application.properties中的配置如下

zf.auto.prefix=hello

zf.auto.suffix=123

具體的測試類

@RestControllerpublic class HelloController { @Autowired HelloService helloService; @RequestMapping('/sayHello') public String sayHello(){ return helloService.sayHello('小福子'); }}

項目訪問路徑:http://localhost:8080/sayHello

SpringBoot如何實現starter原理詳解

好了 ,本文就說到這里,本文相關案例我已經上傳到 碼云 ,小伙伴們可以自行下載:https://gitee.com/xiaofuzi123/springboot-learning-example

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 日本一卡2卡三卡4卡 免费网站仙踪 | 狠狠色婷婷丁香综合久久韩国 | 日本免费一级 | 久久久久免费精品国产 | 日本高清www免费视频 | 精品国产97在线观看 | 网友偷自拍原创区 | 久久99精品久久只有精品 | 被公侵犯肉体中文字幕一区二区 | 欧美日韩在线精品一区二区三区 | 国产美女无遮挡软件 | 日韩精品一区二区三区中文 | 国产黄色自拍视频 | 婷婷色亚洲 | 麻豆传媒2021精品传媒一区 | 国产欧美综合精品一区二区 | 一级毛片免费观看视频 | 大伊香蕉精品二区视频在线 | 国产精品一区三区 | 国产在线播放一区 | 99在线观看视频免费精品9 | 黄视频在线| 亚洲综合日韩欧美一区二区三 | 成年大片免费视频播放手机不卡 | 国产一区二区久久 | 色yeye成人免费视频 | 国产成人在线免费视频 | 色欲综合视频天天天 | 97视频免费播放观看在线视频 | 色综合久久天天综线观看 | 国产曰批视频免费观看完 | 大杳蕉精品视频在线观看 | 99久久精品免费看国产麻豆 | 手机在线看黄色 | 365深夜福利在线观看 | 亚洲欧洲一二三区机械有限公司 | 一级中文字幕 | 国产欧美日韩在线观看一区二区三区 | 天堂激情网 | 美女毛片在线 | 一级黄色小视频 |