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

网友投稿 1951 2022-11-18


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注解,原因是继承ApplicationContextAwarekAuXnKqZT接口是必须实现的方法

@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配置文件中配置一下这个类:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop" xmlns:task="http://springframework.org/schema/task"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.3.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.3.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.3.xsd

http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-4.3.xsd

http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache.xsd">

kAuXnKqZT

scope="singleton">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop" xmlns:task="http://springframework.org/schema/task"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.3.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.3.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.3.xsd

http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-4.3.xsd

http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache.xsd">

kAuXnKqZT

scope="singleton">

scope="singleton">

然后通过这个类提供的方法就能正常的获取在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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java 枚举enum的用法(与在switch中的用法)
下一篇:java、android可用的rtp封包解包h264案例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~