日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

手写一个springboot的starter

發(fā)布時(shí)間:2023/12/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 手写一个springboot的starter 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

starter 自動(dòng)注入組件,為用戶省去組件引入、配置類、jar包沖突解決。starter一般都包含2個(gè)類:ConfigurationProperties和AutoConfiguration。

命名規(guī)則

由于SpringBoot官方本身就提供了很多Starter,為了區(qū)別那些是官方的,哪些是第三方的,所以SpringBoot官方提出:

第三方提供的Starter統(tǒng)一用 xxx-spring-boot-starter

而官方提供的Starter統(tǒng)一用 spring-boot-starter-xxx

手寫starter

項(xiàng)目目錄

1、pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>yzh-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><java.version>1.8</java.version><spring-boot.version>2.4.6</spring-boot.version></properties><dependencies><!-- 用來提示用的 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><!-- 禁止傳遞依賴 --><optional>true</optional><version>${spring-boot.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>${spring-boot.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2、屬性配置類

@ConfigurationProperties(prefix = "yzh.hello") @Data public class HelloProperties {private String name = "default";}

3、業(yè)務(wù)執(zhí)行類

@Data @NoArgsConstructor @AllArgsConstructor public class HelloStarter {private HelloProperties helloProperties;public String hello() {return "welcome " + helloProperties.getName() + "!";}}

4、自動(dòng)裝配類

@ConditionalOnClass(HelloStarter.class) @EnableConfigurationProperties(HelloProperties.class) @Configuration public class HelloAutoConfiguration {@Beanpublic HelloStarter helloStarter(HelloProperties helloProperties) {return new HelloStarter(helloProperties);}}

5、自動(dòng)裝配類被springboot識(shí)別的配置文件

在resource目錄下,新建一個(gè)META-INF文件夾,在META-INF文件夾下新建一個(gè)spring.factories文件。

#聲明配置類的全路徑 org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.yang.HelloAutoConfiguration

??

6、使用 mvn?install 打包

?

7、其它項(xiàng)目引入使用

引入:

<dependency><groupId>org.example</groupId><artifactId>yzh-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency>

?application.properties中的配置:

yzh.hello.name=gouwa

代碼:?

@RestController @RequestMapping("/starter") @Slf4j public class StarterController {@Resourceprivate HelloStarter helloStarter;@GetMapping("/index")public String index() {return helloStarter.hello();} }

?啟動(dòng)后,訪問:http://localhost:8080/starter/index

項(xiàng)目GitHub地址:https://github.com/zihea/yzh-spring-boot-starter

總結(jié)

以上是生活随笔為你收集整理的手写一个springboot的starter的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。