最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springBoot @Enable* 注解的使用

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

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

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

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

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

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

可以看到@Import中,其實(shí)是導(dǎo)入了一個(gè)AutoConfigurationImportSelector的類。最關(guān)鍵的是,該類實(shí)現(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 會(huì)自動(dòng)的被注入,被Spring所管理。

接著來(lái)看 AutoConfigurationImportSelector中 selectImports 的實(shí)現(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中,可以看到 有個(gè)鏈接指向了 ImportBeanDefinitionRegistrar。這同樣是一個(gè)接口,作用跟 ImportSelector 一樣。

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

}

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

代碼示例:

使用@Import編寫自己的@Enable

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

public class Role {
}

public class User {
}

2、自定義Enable注解

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

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

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

4、編寫啟動(dòng)類

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

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

控制臺(tái)成功打印。

代碼示例2,自動(dòng)裝配第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、將項(xiàng)目安裝到本地maven倉(cāng)庫(kù):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、控制臺(tái)打印

com.edu.MyTest$$Lambda$153/284686302@2c07545f

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

相關(guān)文章

  • Java中ScheduledExecutorService介紹和使用案例(推薦)

    Java中ScheduledExecutorService介紹和使用案例(推薦)

    ScheduledExecutorService是Java并發(fā)包中的接口,用于安排任務(wù)在給定延遲后運(yùn)行或定期執(zhí)行,它繼承自ExecutorService,具有線程池特性,可復(fù)用線程,提高效率,本文主要介紹java中的ScheduledExecutorService介紹和使用案例,感興趣的朋友一起看看吧
    2024-10-10
  • slf4j與log4j全面了解

    slf4j與log4j全面了解

    下面小編就為大家?guī)?lái)一篇slf4j與log4j全面了解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-07-07
  • 淺談Spring如何解決循環(huán)依賴的問(wèn)題

    淺談Spring如何解決循環(huán)依賴的問(wèn)題

    這篇文章主要介紹了淺談Spring如何解決循環(huán)依賴的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java中mybatis關(guān)于example類的使用詳解

    Java中mybatis關(guān)于example類的使用詳解

    這篇文章主要介紹了Java中mybatis中關(guān)于example類的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • java web如何解決瞬間高并發(fā)

    java web如何解決瞬間高并發(fā)

    這篇文章主要為大家詳細(xì)介紹了java web解決瞬間高并發(fā)的策略,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Java多線程 ThreadLocal原理解析

    Java多線程 ThreadLocal原理解析

    這篇文章主要介紹了Java多線程 ThreadLocal原理,ThreadLoal 變量,線程局部變量,同一個(gè) ThreadLocal 所包含的對(duì)象,在不同的 Thread 中有不同的副本,下面文章也是圍繞Java多線程 ThreadLocal展開內(nèi)容,需要的朋友可以參考一下
    2021-10-10
  • Springboot項(xiàng)目如何獲取所有的接口

    Springboot項(xiàng)目如何獲取所有的接口

    這篇文章主要介紹了Springboot項(xiàng)目如何獲取所有的接口,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringMVC如何獲取表單數(shù)據(jù)(radio和checkbox)

    SpringMVC如何獲取表單數(shù)據(jù)(radio和checkbox)

    這篇文章主要介紹了SpringMVC如何獲取表單數(shù)據(jù)(radio和checkbox)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • redisson特性及優(yōu)雅實(shí)現(xiàn)示例

    redisson特性及優(yōu)雅實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了redisson特性及優(yōu)雅實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • jvm字符串常量池在什么內(nèi)存區(qū)域問(wèn)題解析

    jvm字符串常量池在什么內(nèi)存區(qū)域問(wèn)題解析

    這篇文章主要介紹了jvm字符串常量池在什么內(nèi)存區(qū)域的問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11

最新評(píng)論

宁强县| 绥宁县| 长乐市| 松原市| 凌云县| 阿克| 钟祥市| 苗栗县| 伊春市| 建宁县| 宜良县| 建水县| 道真| 葵青区| 焉耆| 比如县| 永福县| 河北区| 格尔木市| 平陆县| 阜城县| 衡山县| 博乐市| 天津市| 固安县| 边坝县| 黑龙江省| 久治县| 中牟县| 贵南县| 彭阳县| 长汀县| 南岸区| 工布江达县| 江山市| 万源市| 长治县| 海盐县| 昌邑市| 宜城市| 咸丰县|