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

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

淺談SpringBoot如何自定義Starters

瀏覽:83日期:2022-08-12 08:42:58
目錄一、Starters原理1.1 Starters場(chǎng)景啟動(dòng)器二、自定義Starters三、代碼步驟一、Starters原理1.1 Starters場(chǎng)景啟動(dòng)器

1、場(chǎng)景需要用到的依賴是什么?

比如依賴的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è)配置類@ConditionalOnXXX 在指定條件成立的情況下自動(dòng)配置類生效

自動(dòng)裝配順序在特定自動(dòng)裝配Class之前 @AutoConfigureBefore在特定自動(dòng)裝配Class之后@AutoConfigureAfter指定順序@AutoConfigureOrder

@Bean 給容器中添加組件@ConfigurationPropertie結(jié)合相關(guān)xxxProperties類來(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)配置類要能加載將需要啟動(dòng)就加載的自動(dòng)配置類,將標(biāo)注@Configuration的自動(dòng)配置類配置在META?INF/spring.factories下,自動(dòng)配置類就會(huì)生效

# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,

3、模式

啟動(dòng)器(starter)

淺談SpringBoot如何自定義Starters

啟動(dòng)器只用來(lái)做依賴導(dǎo)入專門寫(xiě)一個(gè)自動(dòng)配置模塊啟動(dòng)器依賴自動(dòng)配置,別人只需要引入啟動(dòng)器(starters)

mybatis-spring-boot-starter 自定義啟動(dòng)器名 -spring-boot-starter

二、自定義Starters

構(gòu)建項(xiàng)目:1.先創(chuàng)建一個(gè)空工程

淺談SpringBoot如何自定義Starters淺談SpringBoot如何自定義Starters

2、創(chuàng)建兩個(gè)模塊分別是啟動(dòng)器starter的maven模塊spring的初始化器創(chuàng)建的自動(dòng)配置模塊

啟動(dòng)器maven模塊

淺談SpringBoot如何自定義Starters

自定義的starters

淺談SpringBoot如何自定義Starters

淺談SpringBoot如何自定義Starters

spring的初始化器創(chuàng)建模塊(創(chuàng)建自動(dòng)配置相關(guān)的模塊)

淺談SpringBoot如何自定義Starters

三、代碼步驟

在啟動(dòng)器starter的pom文件中引入配置類的坐標(biāo)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)配置類

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應(yīng)用才生效@EnableConfigurationProperties(HelloProperties.class)public class HelloServiceAutoConfiguration { @Autowired HelloProperties helloProperties; @Bean public HelloService helloService(){HelloService helloService = new HelloService();helloService.setHelloProperties(helloProperties);return helloService; }}

然后將這兩個(gè)模塊安裝到maven倉(cāng)庫(kù)中先安裝配置模塊因?yàn)閟tarter模塊依賴配置模塊,別人調(diào)用我們的starter模塊就行了

淺談SpringBoot如何自定義Starters

然后將啟動(dòng)器starter也裝到倉(cāng)庫(kù)中,別人就可以用坐標(biāo)引入了

在別的項(xiàng)目中引入自定義的啟動(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)器startercom.ming.prefix=一起學(xué)習(xí)com.ming.suffix=你學(xué)費(fèi)了嗎

測(cè)試

@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內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 亚洲爽妇网| 美女流白浆网站 | 国产日产欧产精品精品推荐小说 | 国产成人免费全部网站 | 久久精品国产99国产 | 欧美一级免费观看 | 免费一级 一片一毛片 | 国产精品免费久久久免费 | 国产合集91合集久久日 | 亚洲大片免费观看 | 久久99亚洲精品久久久久 | 欧美成人在线影院 | 香蕉99国内自产自拍视频 | 久久国产影视 | 夜夜躁狠狠躁日日躁2021 | 亚洲第一欧美 | 正在播放国产大学生情侣 | 一级毛片a免费播放王色 | 在线私拍国产福利精品 | 成人在线观看网址 | 国产精选一区 | 午夜两性试爱视频免费 | 亚洲性xx| 日本一区三区二区三区四区 | 大伊香蕉精品视频在线天堂 | 亚洲黄色免费在线观看 | 久久青青草视频 | 三级视频在线 | hd最新国产人妖ts视频 | 日本一区二区不卡久久入口 | 一级做a爱过程免费视 | 夜夜躁狠狠躁日日躁2021 | 亚洲 欧美 手机 在线观看 | 一区二区三区不卡视频 | 日本特黄a级高清免费酷网 日本特黄特色 | 亚洲精品久久久久久久福利 | 成人资源在线 | 亚洲精品久一区 | 一级片日韩 | 亚洲日本一区二区三区在线 | 九九午夜 |