av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:95日期:2022-08-23 09:08:34

使用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 189155278@qq.com * @date 2016-6-12 * @content * */public class SpringContextUtil implements ApplicationContextAware {private static ApplicationContext applicationContext; // Spring應用上下文 // 下面的這個方法上加了@Override注解,原因是繼承ApplicationContextAware接口是必須實現的方法@Overridepublic 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 scope='singleton'></bean> </beans>

然后通過這個類提供的方法就能正常的獲取在spring中托管的bean了,使用很簡單:

/** * 獲取spring托管的redis連接池 */private JedisPool jedisPool = (JedisPool) SpringContextUtil.getBean('jedisPool');

補充知識:解決Spring中為靜態static的@Resource自動注入失敗的問題

在寫一個單例模塊時,在初始化對象時需要注入靜態的參數,導致spring 暴出

@Resource annotation is not supported on static fields

可以通過將@Resource寫在set方法上,并去除static

以上這篇JAVA解決在@autowired,@Resource注入為null的情況就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Java
主站蜘蛛池模板: 久久久久国产一区二区三区不卡 | 欧美一级在线观看 | 久久综合一区二区 | 亚洲国产aⅴ成人精品无吗 综合国产在线 | 一区观看 | 国产精品一区二区福利视频 | 91在线视频观看 | 久久99一区二区 | 日韩中文字幕在线视频 | www.国产91| 成人欧美一区二区三区在线观看 | 欧美成人综合 | 日本精品在线播放 | 国产免费一区 | 色接久久| 狠狠入ady亚洲精品经典电影 | 国产在线资源 | 日韩毛片| 亚洲性人人天天夜夜摸 | 亚洲高清免费视频 | avmans最新导航地址 | 欧美一区二区三区高清视频 | 自拍偷拍亚洲欧美 | 欧美日韩免费在线 | 国产精品日韩一区 | 精品综合久久久 | 精品国产一区二区三区在线观看 | 91精品一区二区三区久久久久久 | 欧美日韩久久 | 三级在线观看 | 国产精品久久久久久久免费观看 | 日本免费一区二区三区视频 | 亚洲午夜精品在线观看 | 日韩成人在线视频 | 久在线 | 一区二区三区四区不卡视频 | 精品欧美一区二区久久久伦 | 精品无码久久久久久国产 | 亚洲网站在线观看 | 99在线免费观看视频 | 成人三级网址 |