ApplicationContext
對象是Spring
開源框架的上下文對象實例,在項目運行時自動裝載Handler
內的所有信息到內存。傳統的獲取方式有很多種,不過隨著Spring
版本的不斷迭代,官方也慢慢的不建議使用部分方式。下面我簡單介紹一種Spring
官方推薦使用的方式!
免費教程專題
恒宇少年在博客整理三套免費學習教程專題
,由于文章偏多
特意添加了閱讀指南
,新文章以及之前的文章都會在專題內陸續(xù)填充
,希望可以幫助大家解惑更多知識點。
本章目標
基于SpringBoot平臺完成ApplicationContext
對象的獲取,并通過實例手動獲取Spring
管理的bean
.
SpringBoot 企業(yè)級核心技術學習專題
專題 | 專題名稱 | 專題描述 |
---|---|---|
001 | Spring Boot 核心技術 | 講解SpringBoot一些企業(yè)級層面的核心組件 |
002 | Spring Boot 核心技術章節(jié)源碼 | Spring Boot 核心技術簡書每一篇文章碼云對應源碼 |
003 | Spring Cloud 核心技術 | 對Spring Cloud核心技術全面講解 |
004 | Spring Cloud 核心技術章節(jié)源碼 | Spring Cloud 核心技術簡書每一篇文章對應源碼 |
005 | QueryDSL 核心技術 | 全面講解QueryDSL核心技術以及基于SpringBoot整合SpringDataJPA |
006 | SpringDataJPA 核心技術 | 全面講解SpringDataJPA核心技術 |
007 | SpringBoot核心技術學習目錄 | SpringBoot系統的學習目錄,敬請關注點贊!!! |
構建項目
本章項目不需要太多的內容,添加Web依賴就可以了。
ApplicationContextAware
這個接口對象就是我們今天的主角,其實以實現ApplicationContextAware
接口的方式獲取ApplicationContext
對象實例并不是SpringBoot特有的功能,早在Spring3.0x版本之后就存在了這個接口,在傳統的Spring
項目內同樣是可以獲取到ApplicationContext
實例的,下面我們看看該如何編碼才能達到我們的效果呢?
實現ApplicationContextAware接口
創(chuàng)建一個實體類并實現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;
/**
* 獲取Spring上下文對象
* ========================
* Created with IntelliJ IDEA.
* User:恒宇少年
* Date:2017/8/26
* Time:23:25
* 碼云:http://git.oschina.net/jnyqy
* ========================
*/
@Component
public class ApplicationContextProvider
implements ApplicationContextAware
{
/**
* 上下文對象實例
*/
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
對象實例后就可以手動獲取Bean
的注入實例對象,在ApplicationContextProvider
類內我簡單的實現了幾個方法來獲取指定的Bean
實例,當然你可以添加更多的方法來完成更多的業(yè)務邏輯。
如果你是想在非Spring
管理的實體內使用ApplicationContext
還不想采用注入ApplicationContextProvider
來完成實例化,這時我們可以修改ApplicationContext
實例對象為靜態(tài)實例,方法改為靜態(tài)方法,這樣在外部同樣是可以獲取到指定Bean
的實例。如下所示:
@Component
public class ApplicationContextProvider
implements ApplicationContextAware
{
/**
* 上下文對象實例
*/
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
就不會自動調用setApplicationContext
方法來為我們設置上下文實例。
總結
本章內容較少,主要講解了SpringBoot
平臺下采用ApplicationContextAware
的方式完成ApplicationContext
實例的獲取,并通過ApplicationContext
實例完成對Spring
管理的Bean
實例手動獲取。
SpringBoot配套源碼地址:https://gitee.com/hengboy/spring-boot-chapter
SpringCloud配套源碼地址:https://gitee.com/hengboy/spring-cloud-chapter