普通對象使用spring容器中的對象的實現(xiàn)方法
引語:
工作中有時候需要在普通的對象中去調(diào)用spring管理的對象,但是在普通的java對象直接使用@Autowired或者@Resource的時候會發(fā)現(xiàn)被注入的對象是null,會報空指針。我們可以簡單的理解為spring是一個公司,它管理的對象就是它的員工,而普通的java對象是其他公司的員工,如果其他公司要找spring公司的員工一起共事沒有經(jīng)過spring公司的同意肯定是不行的。
解決方式:
方法一:如果這個普通對象可以被spring管理的話,最好是直接交給spring管理,這樣spring管理的bean中注入其他的bean是沒有問題的。
方法二:當(dāng)我們的普通對象沒有辦法交給spring管理的時候,我們可以創(chuàng)建一個公共的springBeanUtil專門為普通對象提供spring的員工(有點像spring公司的外包部門,把對象外包給其他公司使用,哈哈)。
@Service
public class SpringBeanUtil implements ApplicationContextAware {
public static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
}
// 這里使用的是根據(jù)class類型來獲取bean 當(dāng)然你可以根據(jù)名稱或者其他之類的方法 主要是有applicationContext你想怎么弄都可以
public static Object getBeanByClass(Class clazz) {
return applicationContext.getBean(clazz);
}
}
這個util呢,其實就是實現(xiàn)了ApplicationContextAware接口,有小伙伴要問了這個接口是干嘛的?這里給出鏈接地址,ApplicationContextAware參考資料。然后我也將文檔中的解釋給摘錄過來了
public interface ApplicationContextAware extends Aware
Interface to be implemented by any object that wishes to be notified of the ApplicationContext that it runs in.
Implementing this interface makes sense for example when an object requires access to a set of collaborating beans. Note that configuration via bean references is preferable to implementing this interface just for bean lookup purposes.
This interface can also be implemented if an object needs access to file resources, i.e. wants to call getResource, wants to publish an application event, or requires access to the MessageSource. However, it is preferable to implement the more specific ResourceLoaderAware, ApplicationEventPublisherAware or MessageSourceAware interface in such a specific scenario.
Note that file resource dependencies can also be exposed as bean properties of type Resource, populated via Strings with automatic type conversion by the bean factory. This removes the need for implementing any callback interface just for the purpose of accessing a specific file resource.
ApplicationObjectSupport is a convenience base class for application objects, implementing this interface.
大概意思就是說只要實現(xiàn)了ApplicationContextAware接口的類,期望被告知當(dāng)前運行的applicationContext是什么。然后又說了如果是想要獲取資源最好是用ResourceLoaderAware, ApplicationEventPublisherAware or MessageSourceAware 這幾個接口,最后還來了一句我們知道你們要使用這些接口,所以我們幫你弄了一個實現(xiàn)了這些接口的抽象類ApplicationObjectSupport(在spring-context的jar包中)。這里說得很清楚要使用bean的話,實現(xiàn)ApplicationContextAware,因為我們這里不需要使用靜態(tài)資源之類的所以我們就不用spring為我們提供的ApplicationObjectSupport了,有興趣的可以自己研究下。
我們這里簡單的看一下ApplicationContextAware類里面都有啥?
發(fā)現(xiàn)就一個方法,spring初始化的時候會將當(dāng)前的applicationContext傳給ApplicationContextAware的setApplicationContext方法,所以只要實現(xiàn)類將這個applicationContext拿到了,就可以通過class類型或者class的名稱來獲取到spring中的bean了。原理其實很簡單吧。使用的時候我們可以調(diào)用spring中的bean。如下:
Test test = (Test) SpringBeanUtil.getBeanByClass(Test.class);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何在springMVC的controller中獲取request
這篇文章主要介紹了如何在springMVC的controller中獲取request,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12
在SpringBoot項目中使用Spring Cloud Sentinel實現(xiàn)流量控制
隨著微服務(wù)架構(gòu)的流行,服務(wù)之間的調(diào)用變得越來越頻繁和復(fù)雜,流量控制是保障系統(tǒng)穩(wěn)定性的重要手段之一,它可以幫助我們避免因過載而導(dǎo)致的服務(wù)不可用,本文將介紹如何在Spring Boot項目中使用Spring Cloud Sentinel來實現(xiàn)流量控制,需要的朋友可以參考下2024-08-08
mybatis-plus批處理IService的實現(xiàn)示例
這篇文章主要介紹了mybatis-plus批處理IService的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java 如何快速,優(yōu)雅的實現(xiàn)導(dǎo)出Excel
這篇文章主要介紹了Java 如何快速,優(yōu)雅的實現(xiàn)導(dǎo)出Excel,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-03-03
詳談Java中net.sf.json包關(guān)于JSON與對象互轉(zhuǎn)的坑
下面小編就為大家分享一篇Java中net.sf.json包關(guān)于JSON與對象互轉(zhuǎn)的坑,具有很好的參考價值,希望對大家有所幫助2017-12-12

