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

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

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

服務器之家 - 編程語言 - Java教程 - springBoot @Enable* 注解的使用

springBoot @Enable* 注解的使用

2021-05-07 13:16心無私天地寬 Java教程

這篇文章主要介紹了springBoot @Enable* 注解的使用,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

1、為什么使用@springbootapplication注解,即可做到自動配置?

答:@springbootapplication,內部起作用的注解其實有3個。@enableautoconfiguration,@componentscan,@configuration。這篇文章主要是講解@enablexx注解

2、為什么使用了@enableautoconfiguration。當使用了@configurationproperties時,即可自動導入.yml 或者.properties里面的配置項?

答:在@enableautoconfiguration內部,使用了@import注解。導入autoconfigurationimportselector幫助springboot將符合條件的configuration加載到ioc容器中。但是內部其實使用了springfactoriesloader來完成。類似與java spi的功能
,即從/meta-inf/spring.factories加載配置

?
1
2
3
4
5
6
7
@target({elementtype.type})
@retention(retentionpolicy.runtime)
@documented
@inherited
@autoconfigurationpackage
@import({autoconfigurationimportselector.class})
public @interface enableautoconfiguration

可以看到@import中,其實是導入了一個autoconfigurationimportselector的類。最關鍵的是,該類實現了接口importselector

?
1
2
3
4
5
6
7
8
public interface importselector {
 /**
  * select and return the names of which class(es) should be imported based on
  * the {@link annotationmetadata} of the importing @{@link configuration} class.
  */
 string[] selectimports(annotationmetadata importingclassmetadata);
 
}

這是importselector的描述,大概意思就是,該方法返回的bean 會自動的被注入,被spring所管理。

接著來看 autoconfigurationimportselector中 selectimports 的實現

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public string[] selectimports(annotationmetadata annotationmetadata) {
  if(!this.isenabled(annotationmetadata)) {
   return no_imports;
  } else {
   autoconfigurationmetadata autoconfigurationmetadata = autoconfigurationmetadataloader.loadmetadata(this.beanclassloader);
   annotationattributes attributes = this.getattributes(annotationmetadata);
   list<string> configurations = this.getcandidateconfigurations(annotationmetadata, attributes);
   configurations = this.removeduplicates(configurations);
   set<string> exclusions = this.getexclusions(annotationmetadata, attributes);
   this.checkexcludedclasses(configurations, exclusions);
   configurations.removeall(exclusions);
   configurations = this.filter(configurations, autoconfigurationmetadata);
   this.fireautoconfigurationimportevents(configurations, exclusions);
   return stringutils.tostringarray(configurations);
  }
 }

代碼都寫得很清楚。就不解釋了。

在@import中,可以看到 有個鏈接指向了 importbeandefinitionregistrar。這同樣是一個接口,作用跟 importselector 一樣。

?
1
2
3
4
5
public interface importbeandefinitionregistrar {
 public void registerbeandefinitions(
   annotationmetadata importingclassmetadata, beandefinitionregistry registry);
 
}

在registerbeandefinitions方法中,可以用beandefinitionregistry 注入我們想要注入的bean。

代碼示例:

使用@import編寫自己的@enable

1、創建2個測試bean

?
1
2
3
4
5
public class role {
}
 
public class user {
}

2、自定義enable注解

?
1
2
3
4
5
6
@target({elementtype.type})
@retention(retentionpolicy.runtime)
@documented
@import(myenableautoconfig.class)
public @interface enablebean {
}

3、實現自己的enableautoconfiguration類

?
1
2
3
4
5
6
public class myenableautoconfig implements importselector{
 @override
 public string[] selectimports(annotationmetadata importingclassmetadata) {
  return new string[]{"com.xhn2.role","com.xhn2.user"};
 }
}

4、編寫啟動類

?
1
2
3
4
5
6
7
8
9
@enablebean
@componentscan("com.xhn2")
public class main {
 public static void main(string[] args) {
  configurableapplicationcontext context = springapplication.run(main.class, args);
  system.out.println(context.getbean(user.class));
  system.out.println(context.getbean(role.class));
 }
}

5、運行結果

com.xhn2.user@496bc455
com.xhn2.role@59402b8f

控制臺成功打印。

代碼示例2,自動裝配第3方jar包的bean

新建maven工程

1、pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<modelversion>4.0.0</modelversion>
 
 <groupid>org.csp</groupid>
 <artifactid>hello</artifactid>
 <version>1.0.0</version>
 
 <properties>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
  <project.build.sourceencoding>utf-8</project.build.sourceencoding>
 </properties>
 
 <dependencies>
  <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-context</artifactid>
  <version>4.3.17.release</version>
  </dependency>
 </dependencies>

2、編寫configuration

?
1
2
3
4
5
6
7
@configuration
public class mytest {
 @bean
 public runnable runnable() {
  return ()->{};
 }
}

在resources下新建meta-inf/spring.factories文件,加入以下配置

?
1
org.springframework.boot.autoconfigure.enableautoconfiguration=com.edu.mytest

3、將項目安裝到本地maven倉庫:mvn install

4、主工程引入剛才安裝到本地的jar。

?
1
2
3
4
5
<dependency>
   <groupid>org.csp</groupid>
   <artifactid>hello</artifactid>
   <version>1.0.0</version>
  </dependency>

5、獲取剛才配置的runnable

?
1
2
3
4
5
6
7
8
@springbootapplication
public class main {
 public static void main(string[] args) {
  springapplication application = new springapplication(main.class);
  configurableapplicationcontext context = application.run(args);
  system.out.println(context.getbean(runnable.class));
 }
}

6、控制臺打印

com.edu.mytest$$lambda$153/284686302@2c07545f

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://segmentfault.com/a/1190000015188776

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人快手破解版 | 日本色播 | 好湿好紧太硬了我太爽了h 好湿好滑好硬好爽好深视频 | 久久青青草原精品国产软件 | 成人一区二区免费中文字幕 | 色天天综合色天天碰 | 天天天天天干 | 久热这里只有精品99国产6 | 日本另类z0zx高清 | 草草视频免费观看 | 日本aa大片在线播放免费看 | 欧美日韩一区二区三区在线视频 | 精品无码久久久久久久动漫 | 欧美图片另类小说综合 | 99影视在线视频免费观看 | 日韩精品免费一级视频 | 亚洲国产成人资源在线桃色 | 校花被强迫np肉高h 校服下的白嫩小乳尖h1v1 | 无码观看AAAAAAAA片 | 超级碰碰青草免费视频92 | 视频免费看| 我要看黄色毛片 | 欧美肥bb| 欧美成人中文字幕在线看 | 99热精品久久 | a∨79成人网 | 236z最新伦理 | avtt在线播放 | yellow字幕网在线zmzz91 | 亚洲欧美日韩在线观看看另类 | 99国产成人精品2021 | anal22日本人视频 | 果冻传媒91| 果冻传媒新在线观看免费 | 无限观看社区在线视频 | a网在线 | 欧美日韩一区二区三区免费不卡 | 美女光屁股网站 | 国产日本欧美亚洲精品视 | 亚洲高清视频免费 | 久久精品黄AA片一区二区三区 |