激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久国产| 成人毛片视频免费看 | 成人黄色小视频在线观看 | 久久最新免费视频 | 天天草天天干天天 | 精久久久| 一级一级一级一级毛片 | 久草热久| 九九午夜 | 国产亚洲精品美女久久久 | 特一级黄色毛片 | 国产精品亚洲三区 | 亚洲情av | 最新se94se在线欧美 | 日韩电影一区二区 | 成人欧美在线观看 | 二区视频 | 午夜视频在线免费播放 | sese在线视频 | 爽成人777777婷婷 | 国产午夜精品一区二区三区免费 | 国产二区三区在线播放 | 久久成人黄色 | 国产高潮国产高潮久久久91 | 久久无毛 | 久久久久久久久久久久久久av | 中国av免费观看 | 久久免费观看一级毛片 | 福利在线小视频 | 亚洲精品aaaaa | 国内久久久久 | 成人在线精品视频 | 国产一区二区三区高清 | 一级做a爱片毛片免费 | 久久久久国产一区二区三区不卡 | 中文字幕在线观看1 | 久久成年网| 激情毛片 | 黄色视品| 久久精品视频首页 | 自拍亚洲伦理 |