一、starter :場景啟動器
SpringBoot將每個場景都抽取成一個start,通過引入starts,就能使用到相應的場景功能。即使是這樣,SpringBoot也不能囊括開發中所有的場景,我們往往需要自定義starters,來簡化對SpringBoot的使用。
當我們寫好某一個場景的starter,其他開發人員可以直接引用我們寫好的starter,而不需要進行過多的配置。
idea創建java??
二、自定義starters
1、準備
(1)這個場景需要使用到的依賴是什么?
(2)如何編寫自動配置
Spring boot,我們參考?WebMvcAutoConfiguration
源碼:
?
Springboot啟動流程。?
?
?
2、編寫自動配置遵循的規范
idea創建普通java項目,其中,@ConditionalOnXXX 表示符合這個條件,就會去實例化該配置類
@Configuration //指定這個類是一個配置類
@ConditionalOnXXX //在指定條件成立的情況下自動配置類生效
@AutoConfigureAfter //指定自動配置類的順序
@Bean //給容器中添加組件
@ConfigurationPropertie結合相關xxxProperties類來綁定相關的配置
@EnableConfigurationProperties //讓xxxProperties生效加入到容器中自動配置類要能加載
將需要啟動就加載的自動配置類,配置在META‐INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
?
3、編寫自動配置的模式
(1)圖
?
(2) 分析
(3)例如 spring-boot-starter-web
?
?
四、自定義starters項目搭建
1、創建一個空項目
2、新建一個Modules : Maven工程 作為 啟動器
3、新建一個SpringBoot快速啟動模塊 作為 自動配置類
?
4、修改兩個項目的pom依賴文件
(1)?dhu-spring-boot-starter
<groupId>com.dhu.starter</groupId><artifactId>dhu-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version><!--啟動器--><dependencies><!--引入自動配置模塊,其他開發人員只需要引入我們的啟動器就可以--><dependency><groupId>com.dhu.starter</groupId><artifactId>dhu-spring-boot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>
(2)dhu-spring-boot-starter-autoconfigurer
<groupId>com.dhu.starter</groupId><artifactId>dhu-spring-boot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version><name>dhu-spring-boot-starter-autoconfigurer</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><!--引入spring-boot-starter;所有starter的基本配置--><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies>
?
5、編寫代碼:
(1)HelloProperties?
package com.dhu.starter;import org.springframework.boot.context.properties.ConfigurationProperties;/*** @author zhou* @create 2020/6/1*/
@ConfigurationProperties(prefix = "dhu.hello")
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;}
}
?
(2)HelloService
package com.dhu.starter;/*** @author zhou* @create 2020/6/1*/
public class HelloService {private HelloProperties helloProperties;public HelloProperties getHelloProperties() {return helloProperties;}public void setHelloProperties(HelloProperties helloProperties) {this.helloProperties = helloProperties;}public String sayHelloDhu(String name){return helloProperties.getPrefix() + "-" + name + helloProperties.getSuffix();}
}
?
(3)HelloServiceAutoConfiguration
package com.dhu.starter;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;/*** @author zhou* @create 2020/6/1*/
@Configuration
@ConditionalOnWebApplication //web應用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {@Autowiredprivate HelloProperties helloProperties;@Beanpublic HelloService helloService(){HelloService service = new HelloService();service.setHelloProperties(helloProperties);return service;}
}
?
(4)META-INF/spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.dhu.starter.HelloServiceAutoConfiguration
?
6、將這兩個模塊安裝到Maven倉庫
?
五、編寫SpringBoot項目進行測試
1、pom.xml
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--引入自定義的starter--><dependency><groupId>com.dhu.starter</groupId><artifactId>dhu-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency>
2、application.properties
dhu.hello.prefix=DHU
dhu.hello.suffix=WELCOME
?
3、HelloController
?
4、運行結果
?
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态