天天看點

springboot指定上下文_第三十二章:如何擷取SpringBoot項目的applicationContext對象

ApplicationContext對象是Spring開源架構的上下文對象執行個體,在項目運作時自動裝載Handler内的所有資訊到記憶體。傳統的擷取方式有很多種,不過随着Spring版本的不斷疊代,官方也慢慢的不建議使用部分方式。下面我簡單介紹一種Spring官方推薦使用的方式!

免費教程專題

恒宇少年在部落格整理三套免費學習教程專題,由于文章偏多特意添加了閱讀指南,新文章以及之前的文章都會在專題内陸續填充,希望可以幫助大家解惑更多知識點。

本章目标

基于SpringBoot平台完成ApplicationContext對象的擷取,并通過執行個體手動擷取Spring管理的bean.

SpringBoot 企業級核心技術學習專題

專題

專題名稱

專題描述

001

講解SpringBoot一些企業級層面的核心元件

002

Spring Boot 核心技術簡書每一篇文章碼雲對應源碼

003

對Spring Cloud核心技術全面講解

004

Spring Cloud 核心技術簡書每一篇文章對應源碼

005

全面講解QueryDSL核心技術以及基于SpringBoot整合SpringDataJPA

006

全面講解SpringDataJPA核心技術

007

SpringBoot系統的學習目錄,敬請關注點贊!!!

建構項目

本章項目不需要太多的内容,添加Web依賴就可以了。

ApplicationContextAware

這個接口對象就是我們今天的主角,其實以實作ApplicationContextAware接口的方式擷取ApplicationContext對象執行個體并不是SpringBoot特有的功能,早在Spring3.0x版本之後就存在了這個接口,在傳統的Spring項目内同樣是可以擷取到ApplicationContext執行個體的,下面我們看看該如何編碼才能達到我們的效果呢?

實作ApplicationContextAware接口

建立一個實體類并實作ApplicationContextAware接口,重寫接口内的setApplicationContext方法來完成擷取ApplicationContext執行個體的方法,代碼如下所示:

package com.xunmei.api;

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.stereotype.Component;

@Component

public class ApplicationContextProvider

implements ApplicationContextAware

{

private ApplicationContext applicationContext;

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

this.applicationContext = applicationContext;

}

public ApplicationContext getApplicationContext() {

return applicationContext;

}

public Object getBean(String name){

return getApplicationContext().getBean(name);

}

public T getBean(Class clazz){

return getApplicationContext().getBean(clazz);

}

public T getBean(String name,Class clazz){

return getApplicationContext().getBean(name, clazz);

}

}

我們拿到ApplicationContext對象執行個體後就可以手動擷取Bean的注入執行個體對象,在ApplicationContextProvider類内我簡單的實作了幾個方法來擷取指定的Bean執行個體,當然你可以添加更多的方法來完成更多的業務邏輯。

如果你是想在非Spring管理的實體内使用ApplicationContext還不想采用注入ApplicationContextProvider來完成執行個體化,這時我們可以修改ApplicationContext執行個體對象為靜态執行個體,方法改為靜态方法,這樣在外部同樣是可以擷取到指定Bean的執行個體。如下所示:

@Component

public class ApplicationContextProvider

implements ApplicationContextAware

{

private static ApplicationContext applicationContext;

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

this.applicationContext = applicationContext;

}

public static ApplicationContext getApplicationContext() {

return applicationContext;

}

public static Object getBean(String name){

return getApplicationContext().getBean(name);

}

public static T getBean(Class clazz){

return getApplicationContext().getBean(clazz);

}

public static T getBean(String name,Class clazz){

return getApplicationContext().getBean(name, clazz);

}

}

這裡要注意ApplicationContextProvider類上的@Component注解是不可以去掉的,去掉後Spring就不會自動調用setApplicationContext方法來為我們設定上下文執行個體。

總結

本章内容較少,主要講解了SpringBoot平台下采用ApplicationContextAware的方式完成ApplicationContext執行個體的擷取,并通過ApplicationContext執行個體完成對Spring管理的Bean執行個體手動擷取。

作者個人 部落格

使用開源架構 ApiBoot 助你成為Api接口服務架構師