JAVA解決在@autowired,@Resource注入為null的情況
使用SpringMVC或者SSH過程中,有時(shí)可能會遇到這么一個(gè)問題。就是在一個(gè)普通的JAVA類(不是controller也不是action類)中無法注入在spring配置文件中配置的bean。
比如你在一個(gè)普通java類想調(diào)用某個(gè)在spring中配置的service,你會發(fā)現(xiàn)不管你用@Resource還是@Autowired注解都無法注入,對象始終是null。
那是因?yàn)橐话闫胀ǖ腏ava類沒有被spring代理,自然無法通過spring注入相關(guān)的對象。難道這樣就不能調(diào)用了嗎?這里提供下面一個(gè)類來解決這個(gè)問題:
SpringContextUtil
package com.im.utils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* 這個(gè)類是為了解決在普通類調(diào)用service的問題
*
* @ClassName SpringContextUtil
* @Description
* @author kokjuis 189155278@qq.com
* @date 2016-6-12
* @content
*
*/
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; // Spring應(yīng)用上下文
// 下面的這個(gè)方法上加了@Override注解,原因是繼承ApplicationContextAware接口是必須實(shí)現(xiàn)的方法
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
public static Object getBean(String name, Class requiredType)
throws BeansException {
return applicationContext.getBean(name, requiredType);
}
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
public static boolean isSingleton(String name)
throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}
public static Class getType(String name)
throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
}
public static String[] getAliases(String name)
throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
}
然后在spring配置文件中配置一下這個(gè)類:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <!--配置spring工具類 --> <bean id="SpringContextUtil" class="com.im.utils.SpringContextUtil" scope="singleton"></bean> </beans>
然后通過這個(gè)類提供的方法就能正常的獲取在spring中托管的bean了,使用很簡單:
/**
* 獲取spring托管的redis連接池
*/
private JedisPool jedisPool = (JedisPool) SpringContextUtil.getBean("jedisPool");
補(bǔ)充知識:解決Spring中為靜態(tài)static的@Resource自動注入失敗的問題
在寫一個(gè)單例模塊時(shí),在初始化對象時(shí)需要注入靜態(tài)的參數(shù),導(dǎo)致spring 暴出
@Resource annotation is not supported on static fields
可以通過將@Resource寫在set方法上,并去除static
以上這篇JAVA解決在@autowired,@Resource注入為null的情況就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
@CacheEvict 清除多個(gè)key的實(shí)現(xiàn)方式
這篇文章主要介紹了@CacheEvict 清除多個(gè)key的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java中ArrayList和LinkedList之間的區(qū)別_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java中ArrayList和LinkedList之間的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源的實(shí)戰(zhàn)案例
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源的實(shí)戰(zhàn)案例,文中通過示例代碼和圖文展示介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2024-01-01
詳解Mybatis核心類SqlSessionFactory的構(gòu)建
這篇文章主要為大家詳細(xì)介紹了Mybatis核心類SqlSessionFactory的構(gòu)建過程,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12
解決IDEA報(bào)錯(cuò)Caused by: org.springframework.boot.web.se
遇到IDEA啟動報(bào)錯(cuò),可嘗試以下方法:打開項(xiàng)目設(shè)置(Ctrl+Shift+Alt+S),將JDK版本修改為1.8;或者檢查TomCat依賴,若有問題可嘗試刪除,此外,確保每次拉取項(xiàng)目后,maven地址設(shè)置為本地,并且JDK版本設(shè)置為1.8,以上為個(gè)人解決經(jīng)驗(yàn),希望對大家有所幫助2024-09-09
JavaSE面試題之this與super關(guān)鍵字的區(qū)別詳解
this關(guān)鍵字用于引用當(dāng)前對象的引用,super關(guān)鍵字用于引用父類對象的引用,下面這篇文章主要給大家介紹了關(guān)于JavaSE面試題之this與super關(guān)鍵字區(qū)別的相關(guān)資料,需要的朋友可以參考下2023-12-12
Java實(shí)現(xiàn)導(dǎo)入csv的示例代碼
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)導(dǎo)入csv的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03

