色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

使用SpringBoot自定義starter詳解

瀏覽:140日期:2023-03-12 11:22:47
一、新建一個(gè)工程

工程由xxx-sprig-boot-starter和xxx-sprig-boot-starter-configure兩個(gè)模塊組成;

使用SpringBoot自定義starter詳解

xxx-sprig-boot-starter模塊

只用來(lái)做依賴導(dǎo)入 依賴于 xxx-sprig-boot-starter-configure模塊,沒(méi)有實(shí)際代碼

<?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.ander</groupId> <artifactId>ander-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <!--依賴ander-spring-boot-starter-configure工程--> <dependencies><dependency> <groupId>com.ander</groupId> <artifactId>ander-spring-boot-starter-configure</artifactId> <version>0.0.1-SNAPSHOT</version></dependency> </dependencies></project>

使用SpringBoot自定義starter詳解

xxx-sprig-boot-starter-configure模塊

專門(mén)自動(dòng)配置模塊 依賴于spring-boot-starter-web

<?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.3.10.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ander</groupId> <artifactId>ander-spring-boot-starter-configure</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ander-spring-boot-starter-configure</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-web</artifactId></dependency> </dependencies></project>

使用SpringBoot自定義starter詳解

二、xxx-sprig-boot-starter-configure模塊自動(dòng)配置編碼2.1 服務(wù)層編碼

/** * Service層 * * @Author: Ander * @Date: 2021-05-04 */public class HelloService { private HelloServiceProperties helloServiceProperties; public String helloService(String name) {return helloServiceProperties.getPrefix() + ' '+ name + ' ' + helloServiceProperties.getSuffix(); } public HelloServiceProperties getHelloServiceProperties() {return helloServiceProperties; } public void setHelloServiceProperties(HelloServiceProperties helloServiceProperties) {this.helloServiceProperties = helloServiceProperties; }}2.2 屬性配置類編碼

/** * 屬性配置類 * * @Author: Ander * @Date: 2021-05-04 */@ConfigurationProperties(prefix = 'com.ander')public class HelloServiceProperties { private String prefix = 'hi'; private String suffix = 'hello world'; 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; }}2.3 starter自動(dòng)配置類編碼

@EnableConfigurationProperties({HelloServiceProperties.class})作用:讓xxxProperties生效加入到容器中

/** * 自定義starter自動(dòng)配置類 * * @Author: Ander * @Date: 2021-05-04 */@Configuration@ConditionalOnWebApplication // 指定web應(yīng)用才生效@EnableConfigurationProperties({HelloServiceProperties.class})public class HelloServiceAutoConfigure { @Autowired private HelloServiceProperties helloServiceProperties; @Bean public HelloService helloService() {HelloService helloService = new HelloService();helloService.setHelloServiceProperties(helloServiceProperties);return helloService; }}2.4 添加自動(dòng)配置類到META-INF路徑下

使用SpringBoot自定義starter詳解

2.5 將工程安裝到本地

注意先安裝xxx-spring-boot-starter-configure,再安裝xxx-spring-boot-starter

使用SpringBoot自定義starter詳解

三、新建一個(gè)工程測(cè)試自定義starter3.1 編寫(xiě)controller層

/** * starter測(cè)試控制類 * * @Author: Ander * @Date: 2021-05-05 */@RestControllerpublic class StarterTestController { @Autowired private HelloService helloService; @GetMapping('hello') public String hello(String name) {return helloService.helloService(name); }}3.2 編寫(xiě)配置文件

server.port=8888com.ander.prefix=HIcom.ander.suffix=HELLO WORLD

四、測(cè)試結(jié)果4.1 使用starter默認(rèn)配置

使用SpringBoot自定義starter詳解

4.2 使用自定義配置

使用SpringBoot自定義starter詳解

到此這篇關(guān)于使用Spring Boot自定義starter詳解的文章就介紹到這了,更多相關(guān)Spring Boot自定義starter內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美中文字幕在线看 | 宅女深夜福利视频在线 | 美女张开腿让男人桶爽动漫视频 | 一本伊大人香蕉高清在线观看 | 99免费在线观看视频 | 欧美日韩在线视频 | 欧美va在线播放免费观看 | 日本经典在线三级视频 | 国产精品免费久久久免费 | 无国产精品白浆是免费 | 亚洲欧美一区二区三区在饯 | 日本美女一区二区三区 | 手机看片1024精品国产 | 日本欧美一区二区三区不卡视频 | 成年午夜性视频免费播放 | 女人张开腿让男人 | 在线中文字幕精品第5页 | 在线观看亚洲 | 免费观看性欧美一级 | 久草视频在 | 亚洲a级在线观看 | 国产三级在线观看 | 欧美一级成人免费大片 | 精品国产免费第一区二区 | 久久无码av三级 | 黄色美女免费看 | 三级网址在线 | 一区二区三区免费看 | 美国成人免费视频 | 亚洲爱爱爱 | 老司机免费福利午夜入口ae58 | 亚洲欧美卡通成人制服动漫 | 久久精品视频亚洲 | 99视频免费在线 | 亚洲人成毛片线播放 | 国产在线视频一区 | 久草在线观看福利 | 97在线碰碰观看免费高清 | 国产美女啪 | 亚洲精品免费观看 | 日韩一区二区三区在线免费观看 |