欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springBoot @Enable* 注解的使用

 更新時間:2018年06月06日 09:54:34   作者:心無私天地寬  
這篇文章主要介紹了springBoot @Enable* 注解的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

答:@SpringBootApplication,內(nèi)部起作用的注解其實有3個。@EnableAutoConfiguration,@ComponentScan,@Configuration。這篇文章主要是講解@EnableXX注解

2、為什么使用了@EnableAutoConfiguration。當(dāng)使用了@ConfigurationProperties時,即可自動導(dǎo)入.yml 或者.properties里面的配置項?

答:在@EnableAutoConfiguration內(nèi)部,使用了@Import注解。導(dǎo)入AutoConfigurationImportSelector幫助springBoot將符合條件的Configuration加載到IOC容器中。但是內(nèi)部其實使用了SpringFactoriesLoader來完成。類似與java SPI的功能
,即從/META-INF/spring.factories加載配置

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration 

可以看到@Import中,其實是導(dǎo)入了一個AutoConfigurationImportSelector的類。最關(guān)鍵的是,該類實現(xiàn)了接口ImportSelector

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 的實現(xiàn)

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 一樣。

public interface ImportBeanDefinitionRegistrar {
 public void registerBeanDefinitions(
   AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry);

}

在registerBeanDefinitions方法中,可以用BeanDefinitionRegistry 注入我們想要注入的Bean。

代碼示例:

使用@Import編寫自己的@Enable

1、創(chuàng)建2個測試Bean

public class Role {
}

public class User {
}

2、自定義Enable注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(MyEnableAutoConfig.class)
public @interface EnableBean {
}

3、實現(xiàn)自己的EnableAutoConfiguration類

public class MyEnableAutoConfig implements ImportSelector{
 @Override
 public String[] selectImports(AnnotationMetadata importingClassMetadata) {
  return new String[]{"com.xhn2.Role","com.xhn2.User"};
 }
}

4、編寫啟動類

@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、運行結(jié)果

com.xhn2.User@496bc455
com.xhn2.Role@59402b8f

控制臺成功打印。

代碼示例2,自動裝配第3方j(luò)ar包的Bean

新建maven工程

1、pom.xml

<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

@Configuration
public class MyTest {
 @Bean
 public Runnable runnable() {
  return ()->{};
 }
}

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

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.edu.MyTest

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

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

<dependency>
   <groupId>org.csp</groupId>
   <artifactId>hello</artifactId>
   <version>1.0.0</version>
  </dependency>

5、獲取剛才配置的Runnable

@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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決Aop @AfterReturning因返回類型不一致導(dǎo)致無法執(zhí)行切面代碼

    解決Aop @AfterReturning因返回類型不一致導(dǎo)致無法執(zhí)行切面代碼

    這篇文章主要介紹了解決Aop @AfterReturning因返回類型不一致導(dǎo)致無法執(zhí)行切面代碼問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 詳解application.properties和application.yml文件的區(qū)別

    詳解application.properties和application.yml文件的區(qū)別

    這篇文章主要介紹了詳解application.properties和application.yml文件的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • SpringSecurity跨域請求偽造(CSRF)的防護(hù)實現(xiàn)

    SpringSecurity跨域請求偽造(CSRF)的防護(hù)實現(xiàn)

    本文主要介紹了SpringSecurity跨域請求偽造(CSRF)的防護(hù)實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • hutool?工具類基本使用教程

    hutool?工具類基本使用教程

    Hutool?是一個?Java?工具包,也只是一個工具包,它幫助我們簡化每一行代碼,減少每一個方法,讓?Java?語言也可以?“甜甜的”,下面通過本文學(xué)習(xí)下hutool?工具類基本使用教程,感興趣的朋友跟隨小編一起看看吧
    2021-12-12
  • SpringBoot Import及自定義裝配實現(xiàn)方法解析

    SpringBoot Import及自定義裝配實現(xiàn)方法解析

    這篇文章主要介紹了SpringBoot Import及自定義裝配實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • MyBatis中常見的SQL執(zhí)行方式及其使用方法

    MyBatis中常見的SQL執(zhí)行方式及其使用方法

    MyBatis可能很多人都一直在用,但是MyBatis的SQL執(zhí)行流程可能并不是所有人都清楚了,下面這篇文章主要給大家介紹了關(guān)于MyBatis中常見的SQL執(zhí)行方式及其使用的相關(guān)資料,需要的朋友可以參考下
    2023-09-09
  • SSH框架網(wǎng)上商城項目第12戰(zhàn)之添加和更新商品功能

    SSH框架網(wǎng)上商城項目第12戰(zhàn)之添加和更新商品功能

    這篇文章主要介紹了SSH框架網(wǎng)上商城項目第12戰(zhàn)之添加和更新商品功能的實現(xiàn)代碼,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Java基礎(chǔ)之java泛型通配符詳解

    Java基礎(chǔ)之java泛型通配符詳解

    Java 泛型(generics)是 JDK 5 中引入的一個新特性, 泛型提供了編譯時類型安全檢測機制,該機制允許開發(fā)者在編譯時檢測到非法的類型,今天通過本文給大家介紹java泛型通配符的相關(guān)知識,感興趣的朋友一起看看吧
    2021-07-07
  • SpringBoot整合Pulsar的實現(xiàn)示例

    SpringBoot整合Pulsar的實現(xiàn)示例

    本文主要介紹了SpringBoot整合Pulsar的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 使用Java實現(xiàn)構(gòu)建jenkins的多個job并返回構(gòu)建結(jié)果示例

    使用Java實現(xiàn)構(gòu)建jenkins的多個job并返回構(gòu)建結(jié)果示例

    這篇文章主要介紹了使用Java實現(xiàn)構(gòu)建jenkins的多個job并返回構(gòu)建結(jié)果示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05

最新評論