一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot自動配置的實現原理

SpringBoot自動配置的實現原理

2021-06-28 10:19LNMPR源碼研究 Java教程

這篇文章主要介紹了詳解SpringBoot自動配置原理,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、運行原理

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲欧美成人综合在线 | 亚洲精品国产在线观看 | 国产射频放大器 | 国产一区二区精品久久91 | 下雨天小说词枝 | 丝袜白浆 | 天美蜜桃精东乌鸦传媒 | 午夜dj影院在线视频观看完整 | 女人爽到喷水的视频免费 | 日本不卡高清免费v日本 | 国产精品一区二区在线观看完整版 | 国内久久久 | 国产主播福利在线观看 | 撕开老师的丝袜白丝扒开粉嫩的小 | 2019亚洲男人天堂 | 国产乱人乱精一区二区视频密 | 国人精品视频在线观看 | 精品国产美女福利在线 | 日你逼 | 赤色疑惑 | 欧美人人干 | 国产麻豆精品视频 | 俄罗斯一级淫片 | 国产精品亚洲综合久久 | 欧美xxxxx九色视频免费观看 | 国产精品13p| 5566中文字幕亚洲精品 | 欧美另类xxx精品人妖 | 精品免费久久久久久成人影院 | 色视频久久 | 香蕉精品国产高清自在自线 | 忘忧草在线社区WWW日本直播 | 色综色 | 14一15sexvideo日本 | 日韩欧美一区二区三区中文精品 | 91久久青青青国产免费 | 欧美日韩亚洲一区二区三区在线观看 | 国内精品伊人久久大香线焦 | 极品丝袜老师h系列全文阅读 | 日韩精品免费一级视频 | 美女张开腿让我了一夜 |