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

Spring 應(yīng)用上下文獲取 Bean 的常用姿勢實(shí)例總結(jié)

 更新時(shí)間:2020年05月21日 08:48:26   作者:碼農(nóng)小胖哥  
這篇文章主要介紹了Spring 應(yīng)用上下文獲取 Bean,結(jié)合實(shí)例形式總結(jié)分析了Spring 應(yīng)用上下文獲取 Bean的實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Spring 應(yīng)用上下文獲取 Bean 的常用姿勢。分享給大家供大家參考,具體如下:

1. 前言

通常,在Spring應(yīng)用程序中,當(dāng)我們使用 @Bean,@Service,@Controller,@Configuration 或者其它特定的注解將 Bean 注入 Spring IoC 。然后我們可以使用 Spring 框架提供的 @Autowired 或者 JSR250、JSR330 規(guī)范注解來使用由 Spring IoC 管理的 Bean

2. 從應(yīng)用程序上下文中獲取 Bean

今天我們將來學(xué)習(xí)如何從 ApplicationContext 中獲取 Bean 。因?yàn)橛行┣闆r下我們不得不從應(yīng)用程序上下文中來獲取 Bean 。

2.1 獲取所有的 Bean

ApplicationContext 提供了獲取所有已經(jīng)成功注入 Spring IoC 容器的 Bean 名稱的方法 getBeanDefinitionNames() 。然后我們可以借助于其 getBean(String name) 方法使用 Bean 名稱獲取特定的 Bean。 我們使用之前文章中介紹的 CommandLineRunner 接口來打印一下結(jié)果。

 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.context.ApplicationContext;
 
 import java.util.stream.Stream;
 
 /**
 * @author Felordcn
 */
 @SpringBootApplication
 public class WarSpringBootApplication implements CommandLineRunner {
   @Autowired
   private ApplicationContext applicationContext;
 
   public static void main(String[] args) {
     SpringApplication.run(WarSpringBootApplication.class, args);
 
 
   }
 
   @Override
   public void run(String... args) throws Exception {
     String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
 
     Stream.of(beanDefinitionNames).forEach(beanName->{
       System.out.println("beanName : " + beanName);
 
       Object bean = applicationContext.getBean(beanName);
 
       System.out.println("Spring bean : " + bean);
     });
 
   }
 }

運(yùn)行應(yīng)用會(huì)輸出:

2019-11-05 22:15:54.392 INFO 6356 --- [      main] cn.felord.war.WarSpringBootApplication  : Started WarSpringBootApplication in 4.663 seconds (JVM running for 7.58)
 beanName : org.springframework.context.annotation.internalConfigurationAnnotationProcessor
 Spring bean : org.springframework.context.annotation.ConfigurationClassPostProcessor@6c44052e
 beanName : org.springframework.context.annotation.internalAutowiredAnnotationProcessor
 Spring bean : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@5c371e13
 beanName : org.springframework.context.annotation.internalCommonAnnotationProcessor
 Spring bean : org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@530a8454
 beanName : org.springframework.context.event.internalEventListenerProcessor
 Spring bean : org.springframework.context.event.EventListenerMethodProcessor@1e34c607
 beanName : org.springframework.context.event.internalEventListenerFactory
 Spring bean : org.springframework.context.event.DefaultEventListenerFactory@5215cd9a
 beanName : fooController
 Spring bean : cn.felord.war.controller.FooController@31198ceb
 beanName : IServiceImpl
 Spring bean : cn.felord.war.controller.IServiceImpl@51671b08
 <more...>

2.2 通過名稱獲取特定的 Bean

從上面打印的信息我們也能看出來一些端倪。

  • 有的 beanName 是類全限定名。
  • @Component、@Repository、@Service@Controller等注解創(chuàng)建 Bean 時(shí),如果不指定bean名稱,名稱的默認(rèn)規(guī)則是類名的首字母小寫,如 cn.felord.war.controller.FooControllerfooController。如果類名前兩個(gè)或以上個(gè)字母都是大寫,那么名稱與類名一樣,如 cn.felord.war.controller.IServiceImplIServiceImpl
  • @Bean 標(biāo)識(shí)的 Bean 默認(rèn) 為方法名稱。
  • 配置類相關(guān)注解 @Configuration 一般使用類全限定名。

但是請注意:如果你在聲明 Bean 的時(shí)候指定了名稱就只是你指定的名稱 。如果我們熟悉這些規(guī)則,使用上面提到的getBean(String name) 方法不失為一種好辦法。

2.3 通過類型來獲取 Bean

如果我們不清楚我們想要的特定類型 Bean 的名稱,我們可以根據(jù)類型來獲取 Bean 。ApplicationContext 提供了可以加載特定類型的 Bean 的所有 Bean 的方法getBeansOfType()。它將返回 Map <String,Object> 其中鍵是 Bean 名稱,而值是 Bean 的實(shí)際對象。

我們修改 2.1 章節(jié) 例子中的 run 方法:

   @Override
   public void run(String... args) throws Exception {
     Map<String, FooController> beansOfType = applicationContext.getBeansOfType(FooController.class);
 
 
     beansOfType.forEach((beanName,bean)->{
       System.out.println("beanName : " + beanName);
       System.out.println("bean : " + bean);
     });
   }

再次運(yùn)行,控制臺(tái)打印出:

beanName : fooController
 bean : cn.felord.war.controller.FooController@545f80bf

2.4 獲取特定 Bean 聲明注解標(biāo)記的 Bean

ApplicationContextgetBeansWithAnnotation() 方法可以讓我們獲取 @Service,@Controller或任何其它可以用來創(chuàng)建 Bean 的注解創(chuàng)建的 Bean

   @Override
   public void run(String... args) throws Exception {
     Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(Controller.class);
 
     beansWithAnnotation.forEach((beanName,bean)->{
       System.out.println("beanName : " + beanName);
       System.out.println("bean : " + bean);
     });
   }

打印出:

beanName : fooController
 bean : cn.felord.war.controller.FooController@18ca3c62
 beanName : basicErrorController
 bean : org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController@2c0f7678

3. 總結(jié)

在本文中,我們學(xué)習(xí)如何從 Spring 應(yīng)用上下文中獲取所有 Bean 的列表。有時(shí)我們需要檢查我們期望的 Bean 是否在 Spring 上下文中加載,或者我們需要檢查 Spring IoC 聲明的特定的 Bean 。當(dāng)然你可以開啟Spring Boot Actuatorbeans 端點(diǎn)來獲取所有的 Bean 信息。

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計(jì)有所幫助。

  • java讀取文件和寫入文件的方式(簡單實(shí)例)

    java讀取文件和寫入文件的方式(簡單實(shí)例)

    下面小編就為大家?guī)硪黄猨ava讀取文件和寫入文件的方式(簡單實(shí)例)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-10-10
  • 解決IDEA中同項(xiàng)目引用報(bào)紅問題

    解決IDEA中同項(xiàng)目引用報(bào)紅問題

    在IDEA中,如果項(xiàng)目引用報(bào)紅,可能是因?yàn)镮DEA的引用緩存問題,可以通過File->Invalidate Caches/Restart清空緩存并重建索引來解決,這個(gè)方法可以幫助解決同項(xiàng)目中引用找不到的問題,恢復(fù)正常的項(xiàng)目引用,消除報(bào)紅
    2024-09-09
  • Java?將HTML轉(zhuǎn)為XML的詳細(xì)步驟

    Java?將HTML轉(zhuǎn)為XML的詳細(xì)步驟

    這篇文章主要介紹了Java?將HTML轉(zhuǎn)為XML,本文將以html轉(zhuǎn)為xml格式為例,介紹如何實(shí)現(xiàn)轉(zhuǎn)換,以下是詳細(xì)方法及步驟,需要的朋友可以參考下
    2022-06-06
  • SpringCloud微服務(wù)架構(gòu)升級匯總

    SpringCloud微服務(wù)架構(gòu)升級匯總

    這篇文章主要介紹了SpringCloud微服務(wù)架構(gòu)升級匯總,它提倡將單一應(yīng)用程序劃分成一組小的服務(wù),服務(wù)之間互相協(xié)調(diào)、互相配合,為用戶提供最終價(jià)值,需要的朋友可以參考下
    2019-06-06
  • java中List接口與實(shí)現(xiàn)類介紹

    java中List接口與實(shí)現(xiàn)類介紹

    大家好,本篇文章主要講的是java中List接口與實(shí)現(xiàn)類介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Java 類與對象重難點(diǎn)詳解

    Java 類與對象重難點(diǎn)詳解

    類(class)和對象(object)是兩種以計(jì)算機(jī)為載體的計(jì)算機(jī)語言的合稱。對象是對客觀事物的抽象,類是對對象的抽象。類是一種抽象的數(shù)據(jù)類型
    2021-11-11
  • 深入剖析ArrayList的remove方法

    深入剖析ArrayList的remove方法

    這篇文章主要介紹了ArrayList的remove方法使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。</p>
    2021-09-09
  • 一文弄懂fastjson

    一文弄懂fastjson

    fastjson?是一個(gè)java語言編寫的高性能且功能完善的JSON庫,本文主要介紹了fastjson的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-05-05
  • 最新評論