1. 程式人生 > >spring手動獲取容器中的bean

spring手動獲取容器中的bean

只需要定義一個bean,這個bean繼承ApplicationContextAware

如下:

public class SpringContextHolder implements ApplicationContextAware {  
    private static ApplicationContext applicationContext = null;  
  
    /** 
     * 獲取靜態變數中的ApplicationContext. 
     */  
    public static ApplicationContext getApplicationContext() {  
        assertContextInjected();  
        return applicationContext;  
    }  
  
    /** 
     * 從靜態變數applicationContext中得到Bean, 自動轉型為所賦值物件的型別. 
     */  
    @SuppressWarnings("unchecked")  
    public static <T> T getBean(String name) {  
        assertContextInjected();  
        return (T) applicationContext.getBean(name);  
    }  
  
    /** 
     * 從靜態變數applicationContext中得到Bean, 自動轉型為所賦值物件的型別. 
     */  
    public static <T> T getBean(Class<T> requiredType) {  
        assertContextInjected();  
        return applicationContext.getBean(requiredType);  
    }  
  
    /** 
     * 清除SpringContextHolder中的ApplicationContext為Null. 
     */  
    public static void clearHolder() {  
        applicationContext = null;  
    }  
  
    /** 
     * 實現ApplicationContextAware介面, 注入Context到靜態變數中. 
     */  
    @Override  
    public void setApplicationContext(ApplicationContext applicationContext) {  
        SpringContextHolder.applicationContext = applicationContext;  
    }  
  
    /** 
     * 檢查ApplicationContext不為空. 
     */  
    private static void assertContextInjected() {  
        Validate.validState(applicationContext != null,  
                "applicaitonContext屬性未注入, 請在applicationContext.xml中定義SpringContextHolder.");  
    }  
  
}

把上述bean定義到spring.xml裡頭,那麼

Spring容器會檢測容器中的所有Bean,如果發現某個Bean實現了ApplicationContextAware介面,Spring容器會在建立該Bean之後,自動呼叫該Bean的setApplicationContextAware()方法,呼叫該方法時,會將容器本身作為引數傳給該方法——該方法中的實現部分將Spring傳入的引數(容器本身)賦給該類物件的applicationContext例項變數,因此接下來可以通過該applicationContext例項變數來訪問容器本身。

工具使用

SpringContextHolder.getBean("beanName");

相關推薦

spring手動獲取容器bean

只需要定義一個bean,這個bean繼承ApplicationContextAware如下:public class SpringContextHolder implements ApplicationContextAware { private static A

Spring在 IOC 容器 Bean 之間的關係

一、在 Spring IOC 容器中 Bean 之間存在繼承和依賴關係。 需要注意的是,這個繼承和依賴指的是 bean 的配置之間的關係,而不是指實際意義上類與類之間的繼承與依賴,它們不是一個概念。

spring容器啟動之我見(四、獲取容器獲取容器bean)

1.我們經常會發現在我們的service中有註解而在dao上沒有註解 看圖 因為我們在spring容器初始化bean的時候,是把service當做一個bean ,而dao並不是一個bean,這是個人理解,如果有錯誤,歡迎大家指出。 2. 那我們總結一下那些算是spri

三、獲取Spring容器獲取容器Bean物件

  Spring中的容器物件介面是ApplicationContext,其作用就是載入配置檔案,並初始化所有的Bean物件(容器啟動時)。其實現類主要有2個,分別為: ClassPathXmlApplicationContext:載入類路徑下的Spring

Spring容器Bean的生命周期

pub run down xsd 信息 ini exc rop throw Spring生命周期分為以下步驟: 1.instantiate bean 對象實例化 2.populate properties 封裝屬性 3.如果Bean實現BeanNameAware執行setB

Spring 學習(十)——IOC 容器 Bean 的生命週期

IOC 容器中 Bean 的生命週期方法 •Spring IOC 容器可以管理 Bean 的生命週期, Spring 允許在 Bean 生命週期的特定點執行定製的任務. •Spring IOC 容器對 Bean 的生命週期進行管理的過程: –通過構造器或工廠方法建立 Bean 例項

Spring容器Bean的生命週期(init-method destroy-method)

Spring容器中Bean的生命週期 這一篇很詳細的講了Bean生命週期的每一個過程。     我主要是想實現一下init方法核destory方法,因為這個和AOP程式設計的環繞通知有點兒相似的感覺,所以特別來研究一下這兩個方法。      在Spring配置中,i

Spring手動獲取指定Bean

起因 由於專案需要把一個專案的某個功能移植到另一個專案中,但是兩個專案結構不同,新寫的介面不是被Spring容器管理的,沒辦法在介面中使用@Autowired註解自動注入Serivice(Ps.我這裡說的介面是對外介面,不是Java中的interface,實際

SpringBoot線上程獲取容器Bean

在 Servlet API 中有一個 ServletContextListener 介面,它能夠監聽 ServletContext 物件的生命週期,實際上就是監聽 Web 應用的生命週期。

Spring框架介紹和IoC容器Bean配置(Spring框架)

Spring框架整理第一發,Spring框架介紹,HelloWorld,IoC容器介紹,Bean配置 Spring概述      ①Spring為簡化企業級開發而生,使用Spring,JavaBean就可以實現很多以前要靠EJB才能實現的功能。同樣的功能,在EJB中要通過

通過Spring配置文件bean的property賦值

pla 綁定 cli 原因 對象賦值 art contex start nbsp 基本數據類型賦值-通過spring配置文件中bean中的property 擴展-以此方式可以通過配置為連接數據的屬性賦值 1、如果是基本數據類型,可以通過setter方法為對象中的屬性設置初始

Spring框架—— IOC容器Bean的配置

單引號 framework 將不 配置信息 init 字符串連接 生命 release exp 1 IOC和DI ①IOC(Inversion of Control):反轉控制。 在應用程序中的組件需要獲取資源時,傳統的方式是組件主動的從容器中獲取所需要的資源,在這樣的模

Spring Boot - 獲取所有的Bean信息

util one cto str org start oot ner patch 前言 Spring Boot啟動的時候需要加載許多Bean實現最小化配置,本文將嘗試找出Spring啟動後加載的所有Bean信息; 通過ApplicationContext 去獲取所有的Bea

Spring之IOC容器裝配Bean

1、Spring裝配Bean的過程大致如下:     Spring啟動時讀取應用程式提供的bean配置資訊,並在Spring容器中生成一份該Bean的配置資訊登錄檔,再根據該登錄檔例項化Bean,裝配Bean的屬性資訊,Bean之間的依賴關係和Bean的行為配置。需要滿足的三個條件:Sp

spring_(10)IOC容器Bean的生命週期

SpringIOC容器可以管理Bean的生命週期,Spring允許在Bean生命週期的特定點執行定製的任務 SpringIOC容器對Bean的生命週期進行管理的過程: 通過構造器或工廠方法建立Bean例項 為Bean的屬性設定值和對其他Bean的引

spring MVC 獲取request的body體

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="white-space:pre">

Spring 核心 Ioc容器Bean

注意我的 Spring 版本是 5.1.2.RELEASE, 並且主要使用註解配置. 如果你需要使用 xml 配置, 我想這篇文章應該不適合你. IoC容器 IoC就是控制反轉, 也叫依賴注入(DI). 比如, 我們現在有 A B 兩個類, A 類依賴 B 類在容器

如何在手動獲取springbeanSpring ApplicationContextAware獲取上下文)

conf string str over 映射 div pre bean對象 nco 一、目的 寫了一個項目,多個module,然後想在A模塊中實現固定的config註入,當B模塊引用A時候,能夠直接填寫相對應的配置信息就行了。但是遇到一個問題,B引用A時候,A的配置信息總

springboot配置:通過工具類來獲取spring容器bean

**由於公司電腦限制,完全手敲,有單字錯誤望理解** @component @SuppressWarnings("static-access") public class AppContext implements ApplicationContextAware{   &n

實現ApplicationContextAware介面,java(new或者java反射獲取的物件)獲取spring容器bean

本文參考了https://blog.csdn.net/bailinbbc/article/details/76446594,其實是拷貝了很多內容: 在Web應用中,Spring容器通常採用宣告式方式配置產生:開發者只要在web.xml中配置一個Listener,該Listener將會負責初始化S