一、運行原理
spring boot的運行是由注解@enableautoconfiguration提供的。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@target ({elementtype.type}) @retention (retentionpolicy.runtime) @documented @inherited @autoconfigurationpackage @import ({enableautoconfigurationimportselector. class }) public @interface enableautoconfiguration { string enabled_override_property = "spring.boot.enableautoconfiguration" ; class <?>[] exclude() default {}; string[] excludename() default {}; } |
這里的關鍵功能是@import注解。enableautoconfigurationimportselector使用springfactoriesloader.loadfactorynames方法來掃描具有meat-inf/spring.factories文件的jar包(1.5版本以前使用enableautoconfigurationimportselector類,1.5以后這個類廢棄了使用的是autoconfigurationimportselector類),下面是spring-boot-autoconfigure-1.5.4.release.jar下的meat-inf中的spring.factories文件的部分內容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# initializers org.springframework.context.applicationcontextinitializer=\ org.springframework.boot.autoconfigure.sharedmetadatareaderfactorycontextinitializer,\ org.springframework.boot.autoconfigure.logging.autoconfigurationreportlogginginitializer # application listeners org.springframework.context.applicationlistener=\ org.springframework.boot.autoconfigure.backgroundpreinitializer # auto configuration import listeners org.springframework.boot.autoconfigure.autoconfigurationimportlistener=\ org.springframework.boot.autoconfigure.condition.conditionevaluationreportautoconfigurationimportlistener # auto configuration import filters org.springframework.boot.autoconfigure.autoconfigurationimportfilter=\ org.springframework.boot.autoconfigure.condition.onclasscondition # auto configure org.springframework.boot.autoconfigure.enableautoconfiguration=\ org.springframework.boot.autoconfigure.admin.springapplicationadminjmxautoconfiguration,\ org.springframework.boot.autoconfigure.aop.aopautoconfiguration,\ org.springframework.boot.autoconfigure.amqp.rabbitautoconfiguration,\ org.springframework.boot.autoconfigure.batch.batchautoconfiguration,\ org.springframework.boot.autoconfigure.cache.cacheautoconfiguration,\ org.springframework.boot.autoconfigure.cassandra.cassandraautoconfiguration,\ org.springframework.boot.autoconfigure.cloud.cloudautoconfiguration,\ org.springframework.boot.autoconfigure.context.configurationpropertiesautoconfiguration,\ |
里面的類都是自動配置類,springboot會根據這些自動配置類去自動配置環境。
下面我們就自動動手寫一個starter。
二、自定義starter
首先先介紹幾個條件注解。
- @conditionalonbean:當容器里有指定的bean為true
- @conditionalonclass:當類路徑下有指定的類為true
- @conditionalonmissingbean:當容器里沒有指定的bean為true
- @conditionalonproperty:指定的數據是否有指定的值
- ...
了解了條件注解后,我們開始自定義starter。
1、在自定義starter之前先要在maven中填寫依賴。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<?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>cn.miaolovezhen</groupid> <artifactid>spring-boot-starter-test</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>spring-boot-starter-test</name> <description>demo project for spring boot</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 6 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-autoconfigure</artifactid> <version> 1.5 . 4 .release</version> </dependency> </dependencies> </project> |
2、完成testproperties類,這個類定義了默認的屬性值,如該類中,只有一個屬性值msg,默認為world。@configurationproperties注解會定義一個匹配,如果想修改屬性值,可以在application.properties中使用“匹配.屬性=修改的值”進行修改。如test.msg = tan
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@configurationproperties (prefix = "test" ) public class testproperties { private static final string msg = "springboot" ; private string msg = msg; public string getmsg() { return msg; } public void setmsg(string msg) { this .msg = msg; } } |
3、完成服務類。服務類是指主要的功能類,如果沒有springboot,這些服務類在spring中都是需要自己去配置生成的。如springmvc中的dispatcherservlet、mybatis的datasource等。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class testservice { private string msg; public string sayhello(){ return "hello " + msg; } public string getmsg() { return msg; } public void setmsg(string msg) { this .msg = msg; } } |
4、完成自動配置類。自動配置類主要作用是springboot的配置核心,它會寫在meat-inf/spring.factories中,告知springboot在啟動時去讀取該類并根據該類的規則進行配置。
- @enableconfigurationproperties注解根據testproperties類開啟屬性注入,允許在application.properties修改里面的屬性值。
- @conditiononclass會檢測是否存在testservice類
- @conditiononproperty類會查看是否開啟該自動配置。默認開啟(true)。
- @conditiononmissingbean會檢測容器中是否有testservice類的對象,如果沒有則生成一個。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@configuration @enableconfigurationproperties (testproperties. class ) @conditionalonclass (testservice. class ) @conditionalonproperty (prefix = "test" , value = "enabled" , matchifmissing = true ) public class testserviceautoconfiguration { @autowired testproperties testproperties; @bean @conditionalonmissingbean (testservice. class ) public testservice testservice(){ testservice testservice = new testservice(); testservice.setmsg(testproperties.getmsg()); return testservice; } } |
5、最后一步,不要忘記在在meat-inf文件夾中創建spring.factories文件。內容很簡單,告訴springboot去讀取testserviceautoconfiguration類。
1
2
|
org.springframework.boot.autoconfigure.enableautoconfiguration=\ cn.miaolovezhen.testserviceautoconfiguration |
好啦,搞定!下面可以使用maven install命令把starter存到本地,其他springboot項目需要使用這個starter,直接導入就可以啦。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000017780558