SpringBoot自定義starter實(shí)例代碼
一、簡(jiǎn)介
SpringBoot 最強(qiáng)大的功能就是把我們常用的場(chǎng)景抽取成了一個(gè)個(gè)starter(場(chǎng)景啟動(dòng)器),我們通過(guò)引入SpringBoot 為我提供的這些場(chǎng)景啟動(dòng)器,我們?cè)龠M(jìn)行少量的配置就能使用相應(yīng)的功能。即使是這樣,SpringBoot也不能囊括我們所有的使用場(chǎng)景,往往我們需要自定義starter,來(lái)簡(jiǎn)化我們對(duì)SpringBoot的使用。
下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧
二、如何自定義starter
1.實(shí)例
如何編寫自動(dòng)配置 ?
我們參照@WebMvcAutoConfiguration為例,我們看看需要準(zhǔn)備哪些東西,下面是WebMvcAutoConfiguration的部分代碼:
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
@Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
@EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {
@Bean
@ConditionalOnBean({View.class})
@ConditionalOnMissingBean
public BeanNameViewResolver beanNameViewResolver() {
BeanNameViewResolver resolver = new BeanNameViewResolver();
resolver.setOrder(2147483637);
return resolver;
}
}
}
我們可以抽取到我們自定義starter時(shí),同樣需要的一些配置。
@Configuration //指定這個(gè)類是一個(gè)配置類 @ConditionalOnXXX //指定條件成立的情況下自動(dòng)配置類生效 @AutoConfigureOrder //指定自動(dòng)配置類的順序 @Bean //向容器中添加組件 @ConfigurationProperties //結(jié)合相關(guān)xxxProperties來(lái)綁定相關(guān)的配置 @EnableConfigurationProperties //讓xxxProperties生效加入到容器中 自動(dòng)配置類要能加載需要將自動(dòng)配置類,配置在META-INF/spring.factories中 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
模式
我們參照 spring-boot-starter 我們發(fā)現(xiàn)其中沒(méi)有代碼:

我們?cè)诳此膒om中的依賴中有個(gè) springboot-starter
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
我們?cè)倏纯?spring-boot-starter 有個(gè) spring-boot-autoconfigure
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency>
關(guān)于web的一些自動(dòng)配置都寫在了這里 ,所以我們有以下總結(jié):
啟動(dòng)器starter只是用來(lái)做依賴管理
需要專門寫一個(gè)類似spring-boot-autoconfigure的配置模塊
用的時(shí)候只需要引入啟動(dòng)器starter,就可以使用自動(dòng)配置了
命名規(guī)范
官方命名空間
- 前綴:spring-boot-starter-
- 模式:spring-boot-starter-模塊名
- 舉例:spring-boot-starter-web、spring-boot-starter-jdbc
自定義命名空間
- 后綴:-spring-boot-starter
- 模式:模塊-spring-boot-starter
- 舉例:mybatis-spring-boot-starter
三、自定義starter實(shí)例
我們需要先創(chuàng)建兩個(gè)工程 hello-spring-boot-starter 和 hello-spring-boot-starter-autoconfigurer
1. hello-spring-boot-starter
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>com.gf</groupId> <artifactId>hello-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hello-spring-boot-starter</name> <!-- 啟動(dòng)器 --> <dependencies> <!-- 引入自動(dòng)配置模塊 --> <dependency> <groupId>com.gf</groupId> <artifactId>hello-spring-boot-starter-autoconfigurer</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
同時(shí)刪除 啟動(dòng)類、resources下的文件,test文件。
2. hello-spring-boot-starter-autoconfigurer
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>com.gf</groupId> <artifactId>hello-spring-boot-starter-autoconfigurer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hello-spring-boot-starter-autoconfigurer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <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> <!-- 引入spring-boot-starter,所有starter的基本配合 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project>
2. HelloProperties
package com.gf.service;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "gf.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;
}
}
3. HelloService
package com.gf.service;
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String name ) {
return helloProperties.getPrefix()+ "-" + name + helloProperties.getSuffix();
}
}
4. HelloServiceAutoConfiguration
package com.gf.service;
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應(yīng)該生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService() {
HelloService service = new HelloService();
service.setHelloProperties( helloProperties );
return service;
}
}
5. spring.factories
在 resources 下創(chuàng)建文件夾 META-INF 并在 META-INF 下創(chuàng)建文件 spring.factories ,內(nèi)容如下:
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.gf.service.HelloServiceAutoConfiguration
到這兒,我們的配置自定義的starter就寫完了 ,我們把 hello-spring-boot-starter-autoconfigurer、hello-spring-boot-starter 安裝成本地jar包。
三、測(cè)試自定義starter
我們創(chuàng)建個(gè)項(xiàng)目 hello-spring-boot-starter-test,來(lái)測(cè)試系我們寫的stater。
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>com.gf</groupId> <artifactId>hello-spring-boot-starter-test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hello-spring-boot-starter-test</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <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-web</artifactId> </dependency> <!-- 引入自定義starter --> <dependency> <groupId>com.gf</groupId> <artifactId>hello-spring-boot-starter</artifactId> <version>0.0.1-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>
2. HelloController
package com.gf.controller;
import com.gf.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello/{name}")
public String hello(@PathVariable(value = "name") String name) {
return helloService.sayHello( name + " , " );
}
}
3. application.properties
gf.hello.prefix = hi gf.hello.suffix = what's up man ?
我運(yùn)行項(xiàng)目訪問(wèn) http://127.0.0.1:8080/hello/zhangsan,結(jié)果如下:
hi-zhangsan , what's up man ?
源碼下載: https://github.com/gf-huanchupk/SpringBootLearning
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
關(guān)于maven環(huán)境的安裝及maven集成idea環(huán)境的問(wèn)題
Maven 是一個(gè)基于 Java 的工具,所以要做的第一件事情就是安裝 JDK。本文重點(diǎn)給大家介紹關(guān)于maven環(huán)境的安裝及和idea環(huán)境的集成問(wèn)題,感興趣的朋友一起看看吧2021-09-09
java后臺(tái)實(shí)現(xiàn)js關(guān)閉本頁(yè)面,父頁(yè)面指定跳轉(zhuǎn)或刷新操作
這篇文章主要介紹了java后臺(tái)實(shí)現(xiàn)js關(guān)閉本頁(yè)面,父頁(yè)面指定跳轉(zhuǎn)或刷新操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Java?Lombok實(shí)現(xiàn)手機(jī)號(hào)碼校驗(yàn)的示例代碼
手機(jī)號(hào)碼校驗(yàn)通常是系統(tǒng)開(kāi)發(fā)中最基礎(chǔ)的功能之一,本文主要介紹了Java?Lombok實(shí)現(xiàn)手機(jī)號(hào)碼校驗(yàn)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
java實(shí)現(xiàn)ATM取款項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)ATM取款項(xiàng)目的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
兩分鐘解決IntelliJ IDEA中文亂碼問(wèn)題(推薦)
這篇文章主要介紹了兩分鐘解決IntelliJ IDEA中文亂碼問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
關(guān)于kafka消費(fèi)不到遠(yuǎn)程bootstrap-server?數(shù)據(jù)的問(wèn)題
很多朋友遇到kafka消費(fèi)不到遠(yuǎn)程bootstrap-server?數(shù)據(jù)的問(wèn)題,怎么解決這個(gè)問(wèn)題,很多朋友不知所措,下面小編給大家?guī)?lái)了關(guān)于kafka消費(fèi)不到遠(yuǎn)程bootstrap-server?數(shù)據(jù)的問(wèn)題及解決方法,感興趣的朋友跟隨小編一起看看吧2021-11-11

