javascript
Springboot系列-自定义starter
Springboot系列-自定義starter
前言:用過springboot的各位應該都知道,Springboot相對于Spring/SpringMVC要方便的多,為什么呢?這主要還是歸功于Starter,其實Starter也是基于Spring/SpringMVC基礎上實現的,因為Starter帶來了許多的自動化配置,所以在我們開發的時候省了不少力
理解Starter
那么Starter是基于什么才能夠實現眾多自動化配置的呢?其實Starter的核心注解就是@Conditional,當classpath下面存在某一個class時,這個配置才會生效
自定義Starter
定義Starter
1.首先創建一個普通的新的maven項目,創建完成之后添加Starter的自動化配置類如下:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>2.1.8.RELEASE</version> </dependency>配置完之后其實我們可以通過源碼看到,好多自動化配置已經引入進來了
2.配置完成之后創建一個DemoProperties類,用來接收application.properties中注入的值,如下:
@ConfigurationProperties(prefix = "test") public class DemoProperties {private String name = "王先森";private String hobby = "Coding";public String getName() {return name;}public void setName(String name) {this.name = name;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby = hobby;} }以上這個DemoProperties配置類的意思就是直接將application.properties里面的屬性值注入到該實例中,@ConfigurationProperties 類型安全的屬性注入,即將 application.properties 文件中前綴為 javaboy 的屬性注入到這個類對應的屬性上
application.prpoerties配置文件內容如下:
test.name = wxy test.hobby = basketball3.配置完DemoProperties之后,在定義一個DemoService類并定義個方法如下:
public class DemoService {private String name;private String hobby;public String doSomething() {return name + " do " + hobby + " !";}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby = hobby;} }上面這個DemoService意思就是很簡單的一個類,添加了個doSomething方法,之后再對應文件將該類注入之后,傳入屬性值調用該方法
4.接下來要做的是非常關鍵,就是關于自動配置的定義,因為之前我們可能用的都是別人定義的,所以這次需要自己去定義
@Configuration @EnableConfigurationProperties(DemoProperties.class) @ConditionalOnClass(DemoService.class) public class DemoServiceAutoConfiguration {@AutowiredDemoProperties demoProperties;@BeanDemoService demoService(){DemoService demoService = new DemoService();demoService.setName(demoProperties.getName());demoService.setHobby(demoProperties.getHobby());return demoService;} }針對以上一段自動配置,解釋如下:
- Configuration:表示這是一個配置類
- EnableConfigurationProperties:此注解是為了使之前配置的@ConfigurationProperties 生效
- @ConditionalOnClass :表示項目當前classpath下存在DemoService時,后面的配置才會生效
- 自動配置類中首先注入 DemoProperties ,該實例中有在 application.properties 中配置的相關數據
- 提供一個 DemoService 的實例,將 DemoProperties 中的值注入進去
5.自動化配置類基本完成,接下來還需要一個 spring.factories 文件,那么這個文件是干什么用的呢?我們知道Springboot項目中有一個啟動類,該啟動類包含了@SpringBootApplication 注解,這個注解的定義如下:
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = {@Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class} ), @Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication {}根據上面我們可以看出其實SpringBootApplication 是一個組合注解,它包含了如上注解,其最重要和關鍵的三個注解并解釋如下:
- @SpringBootConfiguration:此注解為Springboot配置注解,如下又在該注解中又包含如下注解:
- @EnableAutoConfiguration:開啟自動配置注解,只有開啟了這個自動配置之后自動配置才會生效,其注解又包含如下注解:
- @ComponentScan:這個注解在有一個主要的功能就是配置注解掃描,可以自定義去配置掃描的范圍
6.在 Maven 項目的 resources 目錄下創建一個名為 META-INF 的文件夾,然后在文件夾中創建一個名為 spring.factories 的文件,文件內容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.wxy.entity.DemoServiceAutoConfiguration動化配置類的路徑配置完成,如此之后我們的自動化配置類就算完成了
本地安裝
我們將我們寫好的Starter項目進行打包,如下:
雙擊完成后,這個 Starter 就安裝到我們本地倉庫了,其實這個時候我們從該路徑可以發現他打包成了一個jar包,如下:
那么如何使用它呢?
使用Starter
新建一個普通的 Spring Boot 工程,創建成功之后,加入自定義 Starter 的依賴,如下:
<dependency><groupId>com.example</groupId><artifactId>emptyMavenProject</artifactId><version>1.0-SNAPSHOT</version> </dependency>此時我們引入了上面自定義的 Starter ,也就是說項目中現在有一個默認的 DemoService 實例可以使用,關于此實例的數據,可以在 application.properties 中進行配置,如下:
test.name = Wang sir test.hobby = Coding配置完成后,可選擇在單元測試方法中注入 DemoSerivce 實例來使用,代碼如下:
@SpringBootTest(classes = App.class) class TeststarterApplicationTests {@AutowiredDemoService demoService;@Testpublic void contextLoads() {System.out.println(demoService.doSomething());} }執行運行結果如下:
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.2.1.RELEASE) 2019-12-03 15:16:57.423 INFO 19712 --- [ main] c.e.t.TeststarterApplicationTests : Starting TeststarterApplicationTests on Wangxinyao with PID 19712 (started by Administrator in E:\IDEAWorkSpace\teststarter) 2019-12-03 15:16:57.430 INFO 19712 --- [ main] c.e.t.TeststarterApplicationTests : No active profile set, falling back to default profiles: default 2019-12-03 15:17:00.216 INFO 19712 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-12-03 15:17:00.754 INFO 19712 --- [ main] c.e.t.TeststarterApplicationTests : Started TeststarterApplicationTests in 4.157 seconds (JVM running for 6.18) Wang sir do Coding ! 2019-12-03 15:17:01.054 INFO 19712 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'結語:基本上對于自定義Starter配置的講解就到這里了,通過自定義Starter,可以把之前的項目通過打包注入到新的項目中,便捷開發
總結
以上是生活随笔為你收集整理的Springboot系列-自定义starter的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 同一个路由器共享打印机
- 下一篇: 盘点国内十大免费CDN提供商