第三十二章:如何獲取SpringBoot項(xiàng)目的applicationContext對象

ApplicationContext對象是Spring開源框架的上下文對象實(shí)例,在項(xiàng)目運(yùn)行時(shí)自動(dòng)裝載Handler內(nèi)的所有信息到內(nèi)存。傳統(tǒng)的獲取方式有很多種,不過隨著Spring版本的不斷迭代,官方也慢慢的不建議使用部分方式。下面我簡單介紹一種Spring官方推薦使用的方式!

免費(fèi)教程專題

恒宇少年在博客整理三套免費(fèi)學(xué)習(xí)教程專題,由于文章偏多特意添加了閱讀指南,新文章以及之前的文章都會(huì)在專題內(nèi)陸續(xù)填充,希望可以幫助大家解惑更多知識點(diǎn)。

本章目標(biāo)

基于SpringBoot平臺完成ApplicationContext對象的獲取,并通過實(shí)例手動(dòng)獲取Spring管理的bean.

SpringBoot 企業(yè)級核心技術(shù)學(xué)習(xí)專題


專題 專題名稱 專題描述
001 Spring Boot 核心技術(shù) 講解SpringBoot一些企業(yè)級層面的核心組件
002 Spring Boot 核心技術(shù)章節(jié)源碼 Spring Boot 核心技術(shù)簡書每一篇文章碼云對應(yīng)源碼
003 Spring Cloud 核心技術(shù) 對Spring Cloud核心技術(shù)全面講解
004 Spring Cloud 核心技術(shù)章節(jié)源碼 Spring Cloud 核心技術(shù)簡書每一篇文章對應(yīng)源碼
005 QueryDSL 核心技術(shù) 全面講解QueryDSL核心技術(shù)以及基于SpringBoot整合SpringDataJPA
006 SpringDataJPA 核心技術(shù) 全面講解SpringDataJPA核心技術(shù)
007 SpringBoot核心技術(shù)學(xué)習(xí)目錄 SpringBoot系統(tǒng)的學(xué)習(xí)目錄,敬請關(guān)注點(diǎn)贊??!!

構(gòu)建項(xiàng)目

本章項(xiàng)目不需要太多的內(nèi)容,添加Web依賴就可以了。

ApplicationContextAware

這個(gè)接口對象就是我們今天的主角,其實(shí)以實(shí)現(xiàn)ApplicationContextAware接口的方式獲取ApplicationContext對象實(shí)例并不是SpringBoot特有的功能,早在Spring3.0x版本之后就存在了這個(gè)接口,在傳統(tǒng)的Spring項(xiàng)目內(nèi)同樣是可以獲取到ApplicationContext實(shí)例的,下面我們看看該如何編碼才能達(dá)到我們的效果呢?

實(shí)現(xiàn)ApplicationContextAware接口

創(chuàng)建一個(gè)實(shí)體類并實(shí)現(xiàn)ApplicationContextAware接口,重寫接口內(nèi)的setApplicationContext方法來完成獲取ApplicationContext實(shí)例的方法,代碼如下所示:

package com.xunmei.api;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 獲取Spring上下文對象
 * ========================
 * Created with IntelliJ IDEA.
 * User:恒宇少年
 * Date:2017/8/26
 * Time:23:25
 * 碼云:http://git.oschina.net/jnyqy
 * ========================
 */
@Component
public class ApplicationContextProvider
    implements ApplicationContextAware
{
    /**
     * 上下文對象實(shí)例
     */
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 獲取applicationContext
     * @return
     */
    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通過name獲取 Bean.
     * @param name
     * @return
     */
    public Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    /**
     * 通過class獲取Bean.
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通過name,以及Clazz返回指定的Bean
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}

我們拿到ApplicationContext對象實(shí)例后就可以手動(dòng)獲取Bean的注入實(shí)例對象,在ApplicationContextProvider類內(nèi)我簡單的實(shí)現(xiàn)了幾個(gè)方法來獲取指定的Bean實(shí)例,當(dāng)然你可以添加更多的方法來完成更多的業(yè)務(wù)邏輯。

如果你是想在非Spring管理的實(shí)體內(nèi)使用ApplicationContext還不想采用注入ApplicationContextProvider來完成實(shí)例化,這時(shí)我們可以修改ApplicationContext實(shí)例對象為靜態(tài)實(shí)例,方法改為靜態(tài)方法,這樣在外部同樣是可以獲取到指定Bean的實(shí)例。如下所示:

@Component
public class ApplicationContextProvider
    implements ApplicationContextAware
{
    /**
     * 上下文對象實(shí)例
     */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 獲取applicationContext
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通過name獲取 Bean.
     * @param name
     * @return
     */
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    /**
     * 通過class獲取Bean.
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通過name,以及Clazz返回指定的Bean
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}

這里要注意ApplicationContextProvider類上的@Component注解是不可以去掉的,去掉后Spring就不會(huì)自動(dòng)調(diào)用setApplicationContext方法來為我們設(shè)置上下文實(shí)例。

總結(jié)

本章內(nèi)容較少,主要講解了SpringBoot平臺下采用ApplicationContextAware的方式完成ApplicationContext實(shí)例的獲取,并通過ApplicationContext實(shí)例完成對Spring管理的Bean實(shí)例手動(dòng)獲取。

SpringBoot配套源碼地址:https://gitee.com/hengboy/spring-boot-chapter
SpringCloud配套源碼地址:https://gitee.com/hengboy/spring-cloud-chapter

作者個(gè)人 博客
使用開源框架 ApiBoot 助你成為Api接口服務(wù)架構(gòu)師

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • 來源:關(guān)于Spring IOC (DI-依賴注入)你需要知道的一切作者:zejian Dao層(AccountDa...
    楊井閱讀 5,441評論 0 27
  • 1. 人啊,最怕自我感覺良好。古代的皇帝們,凡是自我感覺良好的,都想長生不老。后來呢,無非塵歸塵土歸土罷了。 2....
    二月維拉閱讀 261評論 0 0
  • 第一次接觸“云”這個(gè)東西,得從四年前我買的第一部智能手機(jī)‘小七’-天語w700(阿里云手機(jī))談起。當(dāng)時(shí)這款手機(jī)提供...
    七歌閱讀 475評論 0 0

友情鏈接更多精彩內(nèi)容