博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
配置springmvc在其他类中(spring容器外)获取注入bean
阅读量:7188 次
发布时间:2019-06-29

本文共 3101 字,大约阅读时间需要 10 分钟。

学习https://github.com/thinkgem/jeesite

今天在写JedisUtils的时候要注入JedisPool,而这个属性被设置为static,@Resource和@Autowired都不可以注入,因为spring不能为静态变量依赖注入。因此需要额外的方法获取spring管理的bean。本文即SpringContextHolder:

1 package com.demo.common.utils; 2  3 import org.apache.commons.lang3.Validate; 4 import org.slf4j.Logger; 5 import org.slf4j.LoggerFactory; 6 import org.springframework.beans.BeansException; 7 import org.springframework.beans.factory.DisposableBean; 8 import org.springframework.context.ApplicationContext; 9 import org.springframework.context.ApplicationContextAware;10 import org.springframework.context.annotation.Lazy;11 import org.springframework.stereotype.Service;12 13 /**14  * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext.15  * Created by Administrator on 2016/2/23.16  */17 @Service18 @Lazy(false)19 public class SpringContextHolder implements ApplicationContextAware ,DisposableBean {20     private static ApplicationContext applicationContext = null;21     private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);22 23     /**24      * 去的存储在静态变量中的ApplicationContext25      * @return26      */27     public static ApplicationContext getApplicationContext(){28         assertContextInjected();29         return applicationContext;30     }31 32     /**33      * 从静态变量applicationContext中去的Bean,自动转型为所复制对象的类型34      * @param name35      * @param 
36 * @return37 */38 public static
T getBean(String name){39 assertContextInjected();40 return (T)applicationContext.getBean(name);41 }42 43 /**44 * 从静态变量applicationContext中去的Bean,自动转型为所复制对象的类型45 * @param requiredType46 * @param
47 * @return48 */49 public static
T getBean(Class
requiredType){50 assertContextInjected();51 return (T)applicationContext.getBean(requiredType);52 }53 54 /**55 * 清楚SpringContextHolder中的ApplicationContext为Null56 */57 public static void clearHolder(){58 if(logger.isDebugEnabled()){59 logger.debug("清楚SpringContextHolder中的ApplicationContext:"+applicationContext);60 }61 applicationContext = null;62 }63 64 65 /**66 * 检查ApplicationContext不为空67 */68 private static void assertContextInjected() {69 Validate.validState(applicationContext!=null,"applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");70 }71 72 /**73 * 实现ApplicationContextAware接口,注入Context到静态变量74 * @param applicationContext75 * @throws BeansException76 */77 @Override78 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {79 SpringContextHolder.applicationContext = applicationContext;80 }81 82 /**83 * 实现DisposableBean接口,在Context关闭时清理静态变量84 * @throws Exception85 */86 @Override87 public void destroy() throws Exception {88 SpringContextHolder.clearHolder();89 }90 }

 

唯有不断学习方能改变! --
Ryan Miao

转载地址:http://neykm.baihongyu.com/

你可能感兴趣的文章
CGI
查看>>
nginx配置http2
查看>>
project.pbxproj 文件的组织及说明
查看>>
Android 网络请求方面的资料
查看>>
ajax跨域的解决办法
查看>>
ZooKeeper管理指南
查看>>
jqGrid随浏览器缩放自适应宽度
查看>>
JavaScript函数补完:splice()数组操作
查看>>
Souce Control Management-EGit
查看>>
长连接的心跳及重连设计
查看>>
ORA-00020: maximum number of processes (1000) 错误处理
查看>>
cas单点登录集群如何优雅的退出
查看>>
[置顶] spring2.5 + struts2 + ibatis2.3.4 框架整合开发
查看>>
第一次作业
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
安装配置管理 之 安装和配置 Java J2SE Development Kit(JDK)
查看>>
ORACLE常用函数实例
查看>>
php 调用webservers 错误,请高手帮助
查看>>