您好,登錄后才能下訂單哦!
本篇文章為大家展示了SpringBoot 中怎么自定義starter,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
1 先創(chuàng)建一個(gè)空項(xiàng)目(自己起名字)
2 在1步驟中創(chuàng)建的空項(xiàng)目中創(chuàng)建兩個(gè)模塊,創(chuàng)建方式如下圖所示
1)創(chuàng)建啟動(dòng)器模塊用maven項(xiàng)目我的啟動(dòng)器名稱為 mao-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.mao.starter</groupId> <artifactId>mao-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <!-- 啟動(dòng)器--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!-- 引入自動(dòng)配置模塊--> <dependency> <groupId>com.mao-starter</groupId> <artifactId>mao-spring-boot-starter-autoconfigurer</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
</project>
2) 創(chuàng)建自動(dòng)配置模塊,我創(chuàng)建的名字為:mao-spring-boot-starter-autoconfigurer如下圖所示 ![](https://oscimg.oschina.net/oscnet/b7c0a779402394cb5783f3f2eae7a5ce0bc.jpg) 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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.21.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.mao-starter</groupId> <artifactId>mao-spring-boot-starter-autoconfigurer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mao-spring-boot-starter-autoconfigurer</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties>
<!-- 所有要引入的基本配置-->
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
</project>
3 創(chuàng)建如下圖的3個(gè)類
helloProperties.java //屬性類
package com.maostarter.mao;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "map.hello")
public class helloProperties {
public String prefix; 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; } private String suffix;
}
HelloService.java //向外提供服務(wù)的類
public class HelloService {
helloProperties helloPropertie; public helloProperties getHelloPropertie() { return helloPropertie; } public void setHelloPropertie(helloProperties helloPropertie) { this.helloPropertie = helloPropertie; } public String sayHelloMao(String name){ return helloPropertie.getPrefix()+'*'+name+helloPropertie.getSuffix(); }
}
helloServiceAutoConfiguration.java //服務(wù)自動(dòng)注入類
@Configuration
@ConditionalOnWebApplication //web應(yīng)用才生效
@EnableConfigurationProperties(helloProperties.class)
public class helloServiceAutoConfiguration {
@Autowired helloProperties helloPropertie; @Bean public HelloService helloService(){ HelloService service = new HelloService(); service.setHelloPropertie(helloPropertie); return service; }
}
4 配置spring.factories文件,如下圖創(chuàng)建方式
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.maostarter.mao.helloServiceAutoConfiguration
5 如何生成starter,在這個(gè)過程有肯能出現(xiàn)各個(gè)問題,一般是jdk版本不對(duì),maven版本問題,出現(xiàn)問題首先仔細(xì)閱讀控制臺(tái)的信息
不要一出現(xiàn)問題就百度
6 測(cè)試剛剛創(chuàng)建的自定義starter創(chuàng)建個(gè)springBoot項(xiàng)目
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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.autostart.test</groupId> <artifactId>autostart</artifactId> <version>0.0.1-SNAPSHOT</version> <name>autostart</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> <dependency> <groupId>com.mao.starter</groupId> <artifactId>mao-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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
6 給之前配置的屬性文件復(fù)制 如下圖所示
7 創(chuàng)建一個(gè)測(cè)試用例
@Controller
public class HelloController {
@Autowired HelloService helloService; @ResponseBody @RequestMapping("/hello") public String Hello(){ return helloService.sayHelloMao("haha"); }
}
8 測(cè)試結(jié)果如下圖所示
上述內(nèi)容就是SpringBoot 中怎么自定義starter,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。