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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于springboot2.5.5自建启动器starter制品库

發布時間:2023/12/3 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于springboot2.5.5自建启动器starter制品库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【README】

本文po出了自建springboot 啟動器步驟;


【1】新建2個starter相關組件

根據? mybatis-spring-boot-starter,我們看到 自建starter需要兩個組件,分別是 xxx-spring-boot-starter, xxx-spring-boot-starter-autoconfigure ;

其中,xxx-spring-boot-starter負責引入 xxx-spring-boot-starter-autoconfigure 依賴; xxx-spring-boot-starter-autoconfigure 負責定義相關配置;

step1,新建空項目 ;

項目名稱為: springbt-08-starter-diy2;

step2,在空項目上新建2個組件,分別為

lisi-spring-boot-starter 和 lisi-spring-boot-starter-autoconfigure;

其中, lisi-spring-boot-starter為 單純的maven項目;

?lisi-spring-boot-starter-autoconfigure 通過 spring Initializer 工具創建;

step3, lisi-spring-boot-starter 引入 lisi-spring-boot-starter-autoconfigure 依賴;

lisi-spring-boot-starter組件的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>com.cmc.starter</groupId><artifactId>lisi-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version><dependencies><!--引入 lisi-spring-boot-starter-autoconfigure 依賴--><dependency><groupId>com.cmc.starter</groupId><artifactId>lisi-spring-boot-starter-autoconfigure</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>

并把測試目標目錄刪除, lisi-spring-boot-starter目錄結構如下:

?step4, lisi-spring-boot-starter-autoconfigure 組件的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.cmc.starter</groupId><artifactId>lisi-spring-boot-starter-autoconfigure</artifactId><version>0.0.1-SNAPSHOT</version><name>lisi-spring-boot-starter-autoconfigure</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies></project>

【2】開發 starter-autoconfigure 啟動器配置組件

step1, 新建 HelloService,HelloProperties, HelloServiceAutoConfiguration ;

package com.cmc.starter.lisi;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "cmc.hello") public class HelloProperties {private String prefix;private String suffix;public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix = prefix;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;} } package com.cmc.starter.lisi;public class HelloService {HelloProperties helloProperties;public String sayHelloCmc(String name) {return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();}public HelloProperties getHelloProperties() {return helloProperties;}public void setHelloProperties(HelloProperties helloProperties) {this.helloProperties = helloProperties;} } package com.cmc.starter.lisi;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration @ConditionalOnWebApplication // web應用才生效 @EnableConfigurationProperties(HelloProperties.class) // 使屬性文件生效 public class HelloServiceAutoConfiguration {@AutowiredHelloProperties helloProperties;@Beanpublic HelloService helloService() {HelloService helloService = new HelloService();helloService.setHelloProperties(helloProperties);return helloService;} }

step2,在資源類路徑下新建 META-INF/spring.factories 屬性文件;添加 自動配置類全限定類名;

# spring.factories# Auto Config org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.cmc.starter.lisi.HelloServiceAutoConfiguration

參考的是 spring-boot-autoconfigure.jar 中的 META-INF/spring.factories的內容;

目錄結構如下:

step3,先把 lisi-spring-boot-starter-autoconfigure 制品庫安裝到本地倉庫

step4,再把 lisi-spring-boot-starter 制品庫安裝到本地倉庫;


【3】自建starter使用

step1, 新建項目,只需要引入 lisi-spring-boot-starter依賴? 即可, 因為 lisi-spring-boot-starter? 引入了 lisi-spring-boot-starter-autoconfigure,傳遞依賴;

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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.cmc</groupId><artifactId>springboot-08-starter-test2</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-08-starter-test2</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><!--引入自定義的starter--><dependency><groupId>com.cmc.starter</groupId><artifactId>lisi-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency><!-- 測試庫 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- springboot web庫 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

step2, 新建測試項目;編寫 Controller,引入 HelloService

import com.cmc.starter.lisi.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class HelloController {@AutowiredHelloService helloService;@GetMapping("/hello")public String hello() {return helloService.sayHelloCmc("zhangsan");} }

step3,HelloServiceAutoConfiguration有兩個屬性前綴后綴,需要配置;在 application.properties 中配置如下:

cmc.hello.prefix=chengdu cmc.hello.suffix=028

step4,啟動springboot項目,并訪問

localhost:8080/hello ;

總結

以上是生活随笔為你收集整理的基于springboot2.5.5自建启动器starter制品库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。