- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- 淺談SpringBoot如何自定義Starters
1、場(chǎng)景需要用到的依賴(lài)是什么?
比如依賴(lài)的jar
2、如何編寫(xiě)自動(dòng)配置?
以WebMvcAutoConfiguration自動(dòng)配置為例:
@Configuration @ConditionalOnWebApplication @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class }) @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10) @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class }) public class WebMvcAutoConfiguration { public static final String DEFAULT_PREFIX = ""; public static final String DEFAULT_SUFFIX = "";
@Configuration指定這是一個(gè)配置類(lèi)
@ConditionalOnXXX 在指定條件成立的情況下自動(dòng)配置類(lèi)生效
自動(dòng)裝配順序
在特定自動(dòng)裝配Class之前 @AutoConfigureBefore
在特定自動(dòng)裝配Class之后@AutoConfigureAfter
指定順序@AutoConfigureOrder
@Bean 給容器中添加組件
@ConfigurationPropertie結合相關(guān)xxxProperties類(lèi)來(lái)綁定相關(guān)的配置
@ConfigurationProperties(prefix = "spring.mvc") public class WebMvcProperties { }
@EnableConfigurationProperties 讓xxxProperties生效加入到容器中
@Configuration @Import(EnableWebMvcConfiguration.class) @EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class }) public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter { }
配置自動(dòng)裝配Bean:
自動(dòng)配置類(lèi)要能加載
將需要啟動(dòng)就加載的自動(dòng)配置類(lèi),將標注@Configuration的自動(dòng)配置類(lèi)配置在META‐INF/spring.factories下,自動(dòng)配置類(lèi)就會(huì )生效
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
3、模式
啟動(dòng)器(starter)
啟動(dòng)器只用來(lái)做依賴(lài)導入
專(zhuān)門(mén)寫(xiě)一個(gè)自動(dòng)配置模塊
啟動(dòng)器依賴(lài)自動(dòng)配置,別人只需要引入啟動(dòng)器(starters)
mybatis-spring-boot-starter 自定義啟動(dòng)器名 -spring-boot-starter
構建項目:
1.先創(chuàng )建一個(gè)空工程
2、創(chuàng )建兩個(gè)模塊分別是啟動(dòng)器starter的maven模塊和spring的初始化器創(chuàng )建的自動(dòng)配置模塊
啟動(dòng)器maven模塊
自定義的starters
spring的初始化器創(chuàng )建模塊(創(chuàng )建自動(dòng)配置相關(guān)的模塊)
在啟動(dòng)器starter的pom文件中引入配置類(lèi)的坐標ming-spring-boot-starter-autoconfigurer
<?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.ming.springboot</groupId> <artifactId>ming-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.ming.springboot</groupId> <artifactId>ming-spring-boot-starter-autoconfigurer</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
寫(xiě)一個(gè)打招呼的功能
package com.ming.springboot; /** * 打招呼的 * */ 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(); } }
HelloProperties 和Helloservice 進(jìn)行屬性綁定的
package com.ming.springboot; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "com.ming") 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; } }
自動(dòng)配置類(lèi)
package com.ming.springboot; 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 { @Autowired HelloProperties helloProperties; @Bean public HelloService helloService(){ HelloService helloService = new HelloService(); helloService.setHelloProperties(helloProperties); return helloService; } }
然后將這兩個(gè)模塊安裝到maven倉庫中
先安裝配置模塊因為starter模塊依賴(lài)配置模塊,別人調用我們的starter模塊就行了
然后將啟動(dòng)器starter也裝到倉庫中,別人就可以用坐標引入了
在別的項目中引入自定義的啟動(dòng)器starter
<!--引入自定義的starter--> <dependency> <groupId>com.ming.springboot</groupId> <artifactId>ming-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
配置application.properties
#自定義啟動(dòng)器starter com.ming.prefix=一起學(xué)習 com.ming.suffix=你學(xué)費了嗎
測試
@Autowired HelloService helloService; @Test public void starterTest(){ String sayHello = helloService.sayHello("自定義starter"); System.out.println(sayHello); }
到此這篇關(guān)于淺談SpringBoot如何自定義Starters的文章就介紹到這了,更多相關(guān)Spring Boot自定義Starters內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站