温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JAVA如何解决在@autowired,@Resource注入为null的情况

发布时间:2020-10-28 18:10:26 来源:亿速云 阅读:2648 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关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 189155278@qq.com
 * @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配置文件中配置一下这个类:

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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中为静态static的@Resource自动注入失败的问题

在写一个单例模块时,在初始化对象时需要注入静态的参数,导致spring 暴出

@Resource annotation is not supported on static fields

可以通过将@Resource写在set方法上,并去除static

关于JAVA如何解决在@autowired,@Resource注入为null的情况就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI