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