spring boot 自定義starter的實現(xiàn)教程
spring boot 使用 starter 解決了很多配置問題, 但是, 他是怎么來解決這些問題的呢?
這里通過一個簡單的例子, 來看一下, starter是怎么來設置默認配置的.
一. 建 starter 項目

自定義的starter, 項目命名規(guī)范是: 自定義名-spring-boot-starter
先來看一下, 我最后的目錄結構

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>
其實只是加入了 spring-boot-autoconfigure
App文件中的main方法, 我注釋掉了, 這個在這里沒有用到
2. 配置屬性對應的接收文件
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 {
//默認配置內容
private static final String MSG = "world";
private String msg = MSG;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
3. 對外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. 對外service與配置對應文件關聯(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
做完這些之后, 通過 mvn clean install , 打包到maven庫里面
二. spring boot 項目使用
新建一個spring boot 項目, 選擇web即可.
目錄結構:

先看一下引用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. 結果: 未配置情況下, 應該是輸出 hello world

2. 在配置文件中, 加入 hello.msg=hahahahahah


這個例子很簡單, 只是顯示一下主要的過程, 別的都是各插件自己的邏輯判斷了.
參考資料:
JavaEE開發(fā)的顛覆者 Spring Boot實戰(zhàn)
以上這篇spring boot 自定義starter的實現(xiàn)教程就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring Boot + Jpa(Hibernate) 架構基本配置詳解
本篇文章主要介紹了Spring Boot + Jpa(Hibernate) 架構基本配置詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
java實現(xiàn)可視化界面肯德基(KFC)點餐系統(tǒng)代碼實例
這篇文章主要介紹了java肯德基點餐系統(tǒng),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05
Java參數校驗中validation和validator的區(qū)別詳解
這篇文章主要介紹了Java參數校驗中validation和validator的區(qū)別詳解,一般對于復雜的業(yè)務參數校驗,可以通過校驗類單獨的校驗方法進行處理,通常對于一些與業(yè)務無關簡單的參數校驗可以采用validation和 validator通過注解的方式實現(xiàn)校驗,需要的朋友可以參考下2023-10-10
關于@Autowierd && @Resource 你真的了解嗎
這篇文章主要介紹了關于@Autowierd && @Resource的具體使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
簡單講解Android開發(fā)中觸摸和點擊事件的相關編程方法
這篇文章主要介紹了Android開發(fā)中觸摸和點擊事件的相關編程方法,包括事件偵聽器等安卓開發(fā)中常用的接口的基本使用方法,需要的朋友可以參考下2015-12-12
java GUI實現(xiàn)ATM機系統(tǒng)(3.0版)
這篇文章主要為大家詳細介紹了java GUI實現(xiàn)ATM機系統(tǒng)(3.0版),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03
SpringMVC源碼之HandlerMapping處理器映射器解析
這篇文章主要介紹了SpringMVC源碼之HandlerMapping處理器映射器解析,在Spring?MVC中,HandlerMapping處理器映射器用于確定請求處理器對象,請求處理器可以是任何對象,只要它們使用了@Controller注解或注解@RequestMapping,需要的朋友可以參考下2023-08-08

