spring boot 自定義starter的實(shí)現(xiàn)教程
spring boot 使用 starter 解決了很多配置問(wèn)題, 但是, 他是怎么來(lái)解決這些問(wèn)題的呢?
這里通過(guò)一個(gè)簡(jiǎn)單的例子, 來(lái)看一下, starter是怎么來(lái)設(shè)置默認(rèn)配置的.
一. 建 starter 項(xiàng)目
自定義的starter, 項(xiàng)目命名規(guī)范是: 自定義名-spring-boot-starter
先來(lái)看一下, 我最后的目錄結(jié)構(gòu)
1. 修改pom.xml文件
<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>org.elvin</groupId> <artifactId>my-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>my-spring-boot-starter</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.9.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
其實(shí)只是加入了 spring-boot-autoconfigure
App文件中的main方法, 我注釋掉了, 這個(gè)在這里沒(méi)有用到
2. 配置屬性對(duì)應(yīng)的接收文件
package org.elvin; import org.springframework.boot.context.properties.ConfigurationProperties;/** * author: Elvin * Date: 2017/12/12 14:51 * Description: */ @ConfigurationProperties(prefix = "hello") public class HelloServiceProperties { //默認(rèn)配置內(nèi)容 private static final String MSG = "world"; private String msg = MSG; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
3. 對(duì)外Service
package org.elvin; /** * author: Elvin * Date: 2017/12/12 14:55 * Description: */ public class HelloService { private String msg; public String sayHello(){ return "Hello " + msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
4. 對(duì)外service與配置對(duì)應(yīng)文件關(guān)聯(lián)
package org.elvin; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * author: Elvin * Date: 2017/12/12 14:59 * Description: */ @Configuration @EnableConfigurationProperties(HelloServiceProperties.class) @ConditionalOnClass(HelloService.class) @ConditionalOnProperty(prefix = "hello", value="enabled", matchIfMissing =true ) public class HelloServiceAutoConfiguration { @Autowired private HelloServiceProperties helloServiceProperties; @Bean @ConditionalOnMissingBean(HelloService.class) public HelloService helloService(){ HelloService helloService = new HelloService(); helloService.setMsg(helloServiceProperties.getMsg()); return helloService; } }
5. starter配置 : spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.elvin.HelloServiceAutoConfiguration
做完這些之后, 通過(guò) mvn clean install , 打包到maven庫(kù)里面
二. spring boot 項(xiàng)目使用
新建一個(gè)spring boot 項(xiàng)目, 選擇web即可.
目錄結(jié)構(gòu):
先看一下引用pom.xml
<dependency> <groupId>org.elvin</groupId> <artifactId>my-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
再看一下HelloController
package org.elvin.learn.springboot.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.elvin.*; /** * author: Elvin * Date: 2017/12/12 15:34 * Description: */ @RestController @RequestMapping("hello") public class HelloController { @Autowired private HelloService helloService; @RequestMapping("index") public String index(){ return helloService.sayHello(); } }
這里的 HelloService 就是 前面自定義 starter 里面的.
1. 結(jié)果: 未配置情況下, 應(yīng)該是輸出 hello world
2. 在配置文件中, 加入 hello.msg=hahahahahah
這個(gè)例子很簡(jiǎn)單, 只是顯示一下主要的過(guò)程, 別的都是各插件自己的邏輯判斷了.
參考資料:
JavaEE開(kāi)發(fā)的顛覆者 Spring Boot實(shí)戰(zhàn)
以上這篇spring boot 自定義starter的實(shí)現(xiàn)教程就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springboot自定義Starter的具體流程
- Spring boot創(chuàng)建自定義starter的完整步驟
- spring boot微服務(wù)自定義starter原理詳解
- springboot自定義starter實(shí)現(xiàn)過(guò)程圖解
- springboot自定義redis-starter的實(shí)現(xiàn)
- SpringBoot自動(dòng)配置之自定義starter的實(shí)現(xiàn)代碼
- 使用SpringBoot自定義starter的完整步驟
- Java SpringBoot自定義starter詳解
- SpringBoot如何自定義starter
- SpringBoot自定義start詳細(xì)圖文教程
相關(guān)文章
解決MyEclipse中的Building workspace問(wèn)題的三個(gè)方法
這篇文章主要介紹了解決MyEclipse中的Building workspace問(wèn)題的三個(gè)方法,需要的朋友可以參考下2015-11-11Java8新日期時(shí)間API的20個(gè)使用示例
這篇文章主要介紹了Java8新日期時(shí)間API的20個(gè)使用示例,為了學(xué)習(xí)Java 8的這個(gè)新庫(kù),這里我創(chuàng)建了20個(gè)以任務(wù)為導(dǎo)向的例子,需要的朋友可以參考下2015-03-03springboot單元測(cè)試兩種方法實(shí)例詳解
這篇文章主要介紹了springboot單元測(cè)試兩種方法實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12java編譯時(shí)出現(xiàn)使用了未經(jīng)檢查或不安全的操作解決方法
這篇文章主要介紹了java編譯時(shí)出現(xiàn)使用了未經(jīng)檢查或不安全的操作的解決方法,需要的朋友可以參考下2014-03-03Mybatis映射文件之常用標(biāo)簽及特殊字符的處理方法
這篇文章主要介紹了Mybatis映射文件常用標(biāo)簽及特殊字符的處理,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05SpringBoot結(jié)合Swagger2自動(dòng)生成api文檔的方法
這篇文章主要介紹了SpringBoot結(jié)合Swagger2自動(dòng)生成api文檔的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05Java中策略設(shè)計(jì)模式的實(shí)現(xiàn)及應(yīng)用場(chǎng)景
策略設(shè)計(jì)模式是Java中一種常用的設(shè)計(jì)模式,它通過(guò)定義一系列算法并將其封裝成獨(dú)立的策略類,從而使得算法可以在不影響客戶端的情況下隨時(shí)切換。策略設(shè)計(jì)模式主要應(yīng)用于系統(tǒng)中存在多種相似的算法、需要靈活調(diào)整算法邏輯或者需要擴(kuò)展新的算法等場(chǎng)景2023-04-04基于java實(shí)現(xiàn)顏色拾色器并打包成exe
這篇文章主要為大家詳細(xì)介紹了如何基于java編寫一個(gè)簡(jiǎn)單的顏色拾色器并打包成exe文件,文中的示例代碼講解詳細(xì),需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10springboot做代理分發(fā)服務(wù)+代理鑒權(quán)的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了springboot做代理分發(fā)服務(wù)+代理鑒權(quán)的實(shí)現(xiàn)過(guò)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01