最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JAVA解決在@autowired,@Resource注入為null的情況

 更新時間:2020年10月09日 15:16:07   作者:Gogym  
這篇文章主要介紹了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)文章

  • java.lang.annotation包詳細介紹

    java.lang.annotation包詳細介紹

    java.lang.annotation?包是 Java 標準庫中的一個核心包,專門用于定義和支持 Java 注解(Annotation),這篇文章主要介紹了java.lang.annotation包介紹,需要的朋友可以參考下
    2024-07-07
  • @CacheEvict 清除多個key的實現(xiàn)方式

    @CacheEvict 清除多個key的實現(xiàn)方式

    這篇文章主要介紹了@CacheEvict 清除多個key的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java中ArrayList和LinkedList之間的區(qū)別_動力節(jié)點Java學院整理

    Java中ArrayList和LinkedList之間的區(qū)別_動力節(jié)點Java學院整理

    這篇文章主要為大家詳細介紹了Java中ArrayList和LinkedList之間的區(qū)別,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • jdk1.8中的for循環(huán)問題記錄

    jdk1.8中的for循環(huán)問題記錄

    這篇文章主要介紹了jdk1.8中的for循環(huán)及jdk1.8 新特性之 forEach 循環(huán)遍歷問題,本文通過實例代碼給大家詳細講解,需要的朋友可以參考下
    2022-11-11
  • SpringBoot實現(xiàn)多數(shù)據(jù)源的實戰(zhàn)案例

    SpringBoot實現(xiàn)多數(shù)據(jù)源的實戰(zhàn)案例

    這篇文章主要介紹了SpringBoot實現(xiàn)多數(shù)據(jù)源的實戰(zhàn)案例,文中通過示例代碼和圖文展示介紹的非常詳細,對大家的學習或工作有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2024-01-01
  • 詳解Mybatis核心類SqlSessionFactory的構(gòu)建

    詳解Mybatis核心類SqlSessionFactory的構(gòu)建

    這篇文章主要為大家詳細介紹了Mybatis核心類SqlSessionFactory的構(gòu)建過程,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2022-12-12
  • 解決IDEA報錯Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded

    解決IDEA報錯Caused by: org.springframework.boot.web.se

    遇到IDEA啟動報錯,可嘗試以下方法:打開項目設(shè)置(Ctrl+Shift+Alt+S),將JDK版本修改為1.8;或者檢查TomCat依賴,若有問題可嘗試刪除,此外,確保每次拉取項目后,maven地址設(shè)置為本地,并且JDK版本設(shè)置為1.8,以上為個人解決經(jīng)驗,希望對大家有所幫助
    2024-09-09
  • Java Files和Paths的使用demo詳解

    Java Files和Paths的使用demo詳解

    Java Files和Paths是Java 7中引入的新API,用于處理文件和目錄,F(xiàn)iles類提供了許多有用的靜態(tài)方法來操作文件和目錄,而Path類則表示文件系統(tǒng)中的路徑,這篇文章主要介紹了Java Files和Paths的使用詳解,需要的朋友可以參考下
    2023-03-03
  • JavaSE面試題之this與super關(guān)鍵字的區(qū)別詳解

    JavaSE面試題之this與super關(guān)鍵字的區(qū)別詳解

    this關(guān)鍵字用于引用當前對象的引用,super關(guān)鍵字用于引用父類對象的引用,下面這篇文章主要給大家介紹了關(guān)于JavaSE面試題之this與super關(guān)鍵字區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • Java實現(xiàn)導(dǎo)入csv的示例代碼

    Java實現(xiàn)導(dǎo)入csv的示例代碼

    這篇文章主要為大家詳細介紹了Java實現(xiàn)導(dǎo)入csv的相關(guān)知識,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以跟隨小編一起學習一下
    2024-03-03

最新評論

玉田县| 舞钢市| 吉水县| 双柏县| 麻栗坡县| 武安市| 双辽市| 保德县| 桐城市| 托克托县| 辉县市| 宁安市| 吉木萨尔县| 巴林左旗| 大名县| 萨嘎县| 新泰市| 咸宁市| 鹤壁市| 儋州市| 隆林| 巩留县| 香格里拉县| 赣榆县| 平舆县| 正镶白旗| 项城市| 金昌市| 九龙县| 延安市| 钟祥市| 宁津县| 巴林右旗| 长葛市| 贵溪市| 于都县| 建瓯市| 泸州市| 临漳县| 浪卡子县| 乌苏市|