JAVA解決在@autowired,@Resource注入為null的情況
使用SpringMVC或者SSH過程中,有時可能會遇到這么一個問題。就是在一個普通的JAVA類(不是controller也不是action類)中無法注入在spring配置文件中配置的bean。
比如你在一個普通java類想調(diào)用某個在spring中配置的service,你會發(fā)現(xiàn)不管你用@Resource還是@Autowired注解都無法注入,對象始終是null。
那是因為一般普通的Java類沒有被spring代理,自然無法通過spring注入相關(guān)的對象。難道這樣就不能調(diào)用了嗎?這里提供下面一個類來解決這個問題:
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;
/**
* 這個類是為了解決在普通類調(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)用上下文
// 下面的這個方法上加了@Override注解,原因是繼承ApplicationContextAware接口是必須實現(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配置文件中配置一下這個類:
<?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>
然后通過這個類提供的方法就能正常的獲取在spring中托管的bean了,使用很簡單:
/**
* 獲取spring托管的redis連接池
*/
private JedisPool jedisPool = (JedisPool) SpringContextUtil.getBean("jedisPool");
補充知識:解決Spring中為靜態(tài)static的@Resource自動注入失敗的問題
在寫一個單例模塊時,在初始化對象時需要注入靜態(tài)的參數(shù),導(dǎo)致spring 暴出
@Resource annotation is not supported on static fields
可以通過將@Resource寫在set方法上,并去除static
以上這篇JAVA解決在@autowired,@Resource注入為null的情況就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot基于redis自定義注解實現(xiàn)后端接口防重復(fù)提交校驗
本文主要介紹了SpringBoot基于redis自定義注解實現(xiàn)后端接口防重復(fù)提交校驗,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
springboot項目編寫發(fā)送異常日志到企微工具包的操作方法
本文介紹了Springboot項目如何編寫發(fā)送異常日志到企業(yè)微信的工具包,內(nèi)容包括創(chuàng)建基礎(chǔ)Bean、配置類、pom依賴等步驟,并展示了如何通過nacos進行配置,這為開發(fā)者提供了一種有效的日志管理方案,方便快速定位和處理項目中的異常問題,感興趣的朋友跟隨小編一起看看吧2024-09-09
Springboot應(yīng)用中過濾器如何修改response的header和body內(nèi)容
這篇文章主要介紹了Springboot應(yīng)用中過濾器如何修改response的header和body內(nèi)容問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Java?Spring框架創(chuàng)建項目與Bean的存儲與讀取詳解
本篇文章將介紹Spring項目的創(chuàng)建,IDEA國內(nèi)源的配置以及Bean的存儲與讀取,所謂的Bean其實就是對象的意思,更詳細(xì)地說Spring Bean是被實例的,組裝的及被Spring 容器管理的Java對象2022-07-07
詳解java操作Redis數(shù)據(jù)庫的redis工具(RedisUtil,jedis工具JedisUtil,JedisPoo
這篇文章主要介紹了java操作Redis數(shù)據(jù)庫的redis工具,包括RedisUtil,jedis工具JedisUtil,JedisPoolUtil工具,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08
Mybatis+Druid+MybatisPlus多數(shù)據(jù)源配置方法
在項目開發(fā)中,經(jīng)常需要連接多個數(shù)據(jù)庫,使用Mybatis、Druid和MybatisPlus可以實現(xiàn)多數(shù)據(jù)源配置,通過定義配置類和修改配置文件,如properties或yaml,可以設(shè)置多個數(shù)據(jù)源,本文介紹了配置項包括Druid基本配置、數(shù)據(jù)源一、數(shù)據(jù)源二,感興趣的朋友一起看看吧2024-09-09

