亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

淺談SpringBoot如何自定義Starters

瀏覽:69日期:2022-08-12 08:42:58
目錄一、Starters原理1.1 Starters場景啟動器二、自定義Starters三、代碼步驟一、Starters原理1.1 Starters場景啟動器

1、場景需要用到的依賴是什么?

比如依賴的jar

2、如何編寫自動配置?

以WebMvcAutoConfiguration自動配置為例:

@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指定這是一個配置類@ConditionalOnXXX 在指定條件成立的情況下自動配置類生效

自動裝配順序在特定自動裝配Class之前 @AutoConfigureBefore在特定自動裝配Class之后@AutoConfigureAfter指定順序@AutoConfigureOrder

@Bean 給容器中添加組件@ConfigurationPropertie結合相關xxxProperties類來綁定相關的配置

@ConfigurationProperties(prefix = 'spring.mvc')public class WebMvcProperties {}

@EnableConfigurationProperties 讓xxxProperties生效加入到容器中

@Configuration@Import(EnableWebMvcConfiguration.class)@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {}

配置自動裝配Bean:自動配置類要能加載將需要啟動就加載的自動配置類,將標注@Configuration的自動配置類配置在META?INF/spring.factories下,自動配置類就會生效

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

3、模式

啟動器(starter)

淺談SpringBoot如何自定義Starters

啟動器只用來做依賴導入專門寫一個自動配置模塊啟動器依賴自動配置,別人只需要引入啟動器(starters)

mybatis-spring-boot-starter 自定義啟動器名 -spring-boot-starter

二、自定義Starters

構建項目:1.先創建一個空工程

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

2、創建兩個模塊分別是啟動器starter的maven模塊spring的初始化器創建的自動配置模塊

啟動器maven模塊

淺談SpringBoot如何自定義Starters

自定義的starters

淺談SpringBoot如何自定義Starters

淺談SpringBoot如何自定義Starters

spring的初始化器創建模塊(創建自動配置相關的模塊)

淺談SpringBoot如何自定義Starters

三、代碼步驟

在啟動器starter的pom文件中引入配置類的坐標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>

寫一個打招呼的功能

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 進行屬性綁定的

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; }}

自動配置類

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; }}

然后將這兩個模塊安裝到maven倉庫中先安裝配置模塊因為starter模塊依賴配置模塊,別人調用我們的starter模塊就行了

淺談SpringBoot如何自定義Starters

然后將啟動器starter也裝到倉庫中,別人就可以用坐標引入了

在別的項目中引入自定義的啟動器starter

<!--引入自定義的starter--><dependency> <groupId>com.ming.springboot</groupId> <artifactId>ming-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version></dependency>

配置application.properties

#自定義啟動器startercom.ming.prefix=一起學習com.ming.suffix=你學費了嗎

測試

@Autowired HelloService helloService; @Test public void starterTest(){String sayHello = helloService.sayHello('自定義starter');System.out.println(sayHello); }

到此這篇關于淺談SpringBoot如何自定義Starters的文章就介紹到這了,更多相關Spring Boot自定義Starters內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 精品视频一区二区三区 | 成年人黄色大片大全 | 国产亚洲精品视频中文字幕 | 午夜在线亚洲 | 综合久久久久综合 | 久久综合欧美 | 国产欧美日韩视频 | 57pao一国产成视频永久免费 | 欧美做爱毛片 | www亚洲 | 九九视频免费精品视频免费 | 好看的毛片| 999国产高清在线精品 | 亚洲精品123区在线观看 | 久久青青草原国产精品免费 | 欧美日韩国产成人高清视频 | 日本无翼乌邪恶大全彩h污污 | 亚洲日本人成网站在线观看 | 青青青青久久精品国产h | 一区二区三区国产 | 伊人久久久综在合线久久在播 | 国产一精品一aⅴ一免费 | 国产亚洲精品看片在线观看 | 亚洲综合欧美日韩 | 国产亚洲女在线线精品 | 午夜黄色一级片 | 久久久久激情免费观看 | 在线观看中文字幕2021 | 国产免费小视频 | 在线看片 在线播放 | 日批视频网址免费观看 | 日日干夜夜干 | 免费观看黄色网址 | 国产无套普通话对白 | 999精品视频在线 | 亚洲精品亚洲人成在线播放 | 久久久精品久久久久特色影视 | 国产成人精品永久免费视频 | 日韩精品在线一区二区 | 亚洲欧美在线观看首页 | 五十路一区二区三区视频 |