您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何使用SpringBoot自定義starter,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
springboot一種全新的編程規(guī)范,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,SpringBoot也是一個服務(wù)于框架的框架,服務(wù)范圍是簡化配置文件。
工程由xxx-sprig-boot-starter
和xxx-sprig-boot-starter-configure
兩個模塊組成;
xxx-sprig-boot-starter
模塊
只用來做依賴導(dǎo)入
依賴于 xxx-sprig-boot-starter-configure
模塊,沒有實際代碼
<?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>
xxx
-sprig-boot-starter-configure
模塊
專門自動配置模塊
依賴于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>
/** * 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; } }
/** * 屬性配置類 * * @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; } }
@EnableConfigurationProperties({HelloServiceProperties.class})
作用:讓xxxProperties生效加入到容器中
/** * 自定義starter自動配置類 * * @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; } }
注意先安裝xxx-spring-boot-starter-configure
,再安裝xxx-spring-boot-starter
/** * starter測試控制類 * * @Author: Ander * @Date: 2021-05-05 */ @RestController public class StarterTestController { @Autowired private HelloService helloService; @GetMapping("hello") public String hello(String name) { return helloService.helloService(name); } }
server.port=8888
com.ander.prefix=HI
com.ander.suffix=HELLO WORLD
感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何使用SpringBoot自定義starter”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。