1. 程式人生 > >JAVA解決在@autowired,@Resource注入為null的問題

JAVA解決在@autowired,@Resource注入為null的問題

使用SpringMVC或者SSH過程中,有時可能會遇到這麼一個問題。就是在一個普通的JAVA類(不是controller也不是action類)中無法注入在spring配置檔案中配置的bean。比如你在一個普通java類想呼叫某個在spring中配置的service,你會發現不管你用@Resource還是@Autowired註解都無法注入,物件始終是null。那是因為一般普通的Java類沒有被spring代理,自然無法通過spring注入相關的物件。難道這樣就不能呼叫了嗎?這裡提供下面一個類來解決這個問題:

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;

/**
 * 這個類是為了解決在普通類呼叫service的問題
 * 
 * @ClassName SpringContextUtil
 * @Description
 * @author kokjuis 
[email protected]
* @date 2016-6-12 * @content * */ public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; // Spring應用上下文 // 下面的這個方法上加了@Override註解,原因是繼承ApplicationContextAware介面是必須實現的方法 @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");



相關推薦

JAVA解決在@autowired,@Resource注入null的問題

使用SpringMVC或者SSH過程中,有時可能會遇到這麼一個問題。就是在一個普通的JAVA類(不是controller也不是action類)中無法注入在spring配置檔案中配置的bean。比如你在一個普通java類想呼叫某個在spring中配置的service,你會發

Spring_關於@Resource注入null解決辦法

初學spring,我在dao層初始化c3p0的時候,使用@Resource註解新建物件是發現注入為null,告訴我 java.lang.NullPointerException。 @Repository public class UserDaoImpl implements UserDao

解決非controller使用@Autowired註解注入null問題

在SpringMVC框架中,我們經常要使用@Autowired註解注入Service或者Mapper介面,我們也知道,在controller層中注入service介面,在service層中注入其它的service介面或者mapper介面都是可以的,但是如果我們要在我們自己封

解決 SpringMvc 非controller類使用@Autowired註解 service注入null的問題

使用SpringMVC框架,在開發的過程中有一些工具類、靜態非controller類需要呼叫由spring管理的service層。但是使用@Autowired註解注入Service,會報null異常;搜尋了下網上的一些方法,又實際測試了下,發現網上說的還缺少了點東西,所以

Spring @Autowired註解在非Controller注入null,Springboot @Reference注入null解決方案

今天使用activiti的執行流程,使用dubbo想要去呼叫service,發現@Reference為null,研究了好久,嘗試直接連線dao層,注入的也為null。. 可能是我的這個不是controller裡面的 解決辦法 通過新增三個關鍵地方即可解決: 1、在cl

對於 Spring @Autowired 或者 @Resource註解null的問題

實現 方式 null 發現 source red res 文件 需要 使用Spring基本都會用到@Autowired 或者 @Resource註解來實現註入,今天做個小東西,需要用到個功能,不想自己寫,就在網上找了個源代碼,然後運行的時候就發現@Autowired註入一直

spring自定義類中@AutoWired標識的元素注入null

最近在做專案的時候,發現程式執行的時候有一個nullpointer exception,一臉懵逼因為感覺程式沒什麼邏輯。後來發現是因為new出來的component不會自動注入它的元素。 現象:@Component修飾的自定義普通類中@Autowired屬性為null 原因:如果是通過new例項化的物件,

@Autowired注入null問題分析

 2018-11-24 22:57:33 問題說明 最近看到Spring事務,在學習過程中遇到一個很苦惱問題               &nbs

Spring boot下@Autowired 注入 NULL的問題

問題描述:在springboot專案中整合quartz時,需要使用到一個import org.springframework.scheduling.quartz.SchedulerFactoryBean這個類,並需要自動注入,在測試程式碼時,發現無法注入到普通.class檔案中,後臺報錯提示:空指標異常。通過

Spring @Autowired註解在非Controller中注入null

前言 好久沒寫部落格了,不知不覺大學已經過去兩年了。 時光快似箭,光陰似如梭, 小學時的打油詩, 逃:) 話說回來,今天在寫一個工具類,裡面用了@Autowired注入了TokenRepository以及TokenService時,在TokenReposi

關於@Component下@Autowired注入null的問題

關於@Component下@Autowired注入為null的問題@TOC @Component public class TimingTaskController{ public static TimingTaskController timingTaskController;

【spring+quartz定時任務】Quartz Job未例項化導致Spring @Autowired 注入null

寫在前面 上週組長佈置給我一個任務,在 Java 多資料來源的配置一文中也提到過,大概就是一個從別人的資料庫中查詢出自己需要的資料,然後存到本地的資料庫中的流程。本以為就一個java多資料來源的知識點,最後需求又更新為獨立開發一個子專案來做這件事情,而子專案需

SSM SpringMVC 非Controller類使用@Autowired註解Servicenull解決辦法

在SSM專案開發中,遇到了一個問題,在非Controller類中使用@Autowired註解的Service類一直報NullPointerException,經過一番搜尋,由於這個專案情況比較特殊,網上的說的大部分解決方法基本大同小異,並不能解決我的問題,但有了

spring static 註解注入null解決辦法

1.頂部使用註解:@Component   2.@Autowired   private static RedisUtil redis;修改為   private static RedisUtil redis;    @Autowired    private GetSpid

Java的if判斷物件null時,null放在比較運算子的左邊還是右邊較好?

  如java中:if(name == null)和if(null == name)有什麼講究嗎?   答:在java裡面,它們是一樣的。但是通常寫為null == name。這其實是在C語言裡面引申出來的。   在C語言裡面,為了防止少敲一個等號,程式設計人員仍然能在編譯的時候找到錯誤。因為if(nam

java 解決Could not locate executable null\bin\winutils.exe in the Hadoop bin

問題描述 在windows環境下執行連線hadoop服務的程式報告以下錯誤資訊: og4j:ERROR Could not find value for key log4j.appender.logRollingFile log4j:ERROR Could not instanti

java中將物件賦值null,對垃圾回收有用嗎?

相信,網上很多java效能優化的帖子裡都會有這麼一條:儘量把不使用的物件顯式得置為null.這樣有助於記憶體回收      可以明確的說,這個觀點是基本錯誤的.sun jdk遠比我們想象中的機智.完全能判斷出物件是否已經no ref..但是,我上面用的詞是"

Android GPS室內定位問題的解決方法(locationnull

為什麼室內沒有location呢?        因為我們開發的時候幾乎肯定都是在室內的,這個時候衛星你是搜尋不到的,所以必然是定位不了的,所以系統如何將位置資訊通知給你的程式。所以要從根本上解決這個問題,就要解決位置資訊獲取問題。         那麼我來告訴大家,只

Java物件vo中屬性null的轉為""

背景:之所以寫這個方法,是因為在用ireport進行列印的時候,如果你查詢的結果,實體中的屬性為null,那麼列印預覽的時候會顯示一個null,按照要求null是不應該顯示,只需要顯示空白就可以。寫的這個方法就解決了列印時查詢的結果中有的實體的屬性值為null的

php 解決json輸出中文null

今天使用json_encode函式,發現中文竟成了null。 原因分析:使用json_encode函式應應使用utf-8編碼,我的頁面用的是gbk. 解決:在json_encode函式前使用iconv('gbk','utf8')函式。函式 function gbk2utf8