配置springmvc在其他类中(spring容器外)获取注入bean

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:

今天在写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 @Service
18 @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      * 去的存储在静态变量中的ApplicationContext
25      * @return
26      */
27     public static ApplicationContext getApplicationContext(){
28         assertContextInjected();
29         return applicationContext;
30     }
31 
32     /**
33      * 从静态变量applicationContext中去的Bean,自动转型为所复制对象的类型
34      * @param name
35      * @param <T>
36      * @return
37      */
38     public static <T> T getBean(String name){
39         assertContextInjected();
40         return (T)applicationContext.getBean(name);
41     }
42 
43     /**
44      * 从静态变量applicationContext中去的Bean,自动转型为所复制对象的类型
45      * @param requiredType
46      * @param <T>
47      * @return
48      */
49     public static <T> T getBean(Class<T> requiredType){
50         assertContextInjected();
51         return (T)applicationContext.getBean(requiredType);
52     }
53 
54     /**
55      * 清楚SpringContextHolder中的ApplicationContext为Null
56      */
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 applicationContext
75      * @throws BeansException
76      */
77     @Override
78     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
79         SpringContextHolder.applicationContext = applicationContext;
80     }
81 
82     /**
83      * 实现DisposableBean接口,在Context关闭时清理静态变量
84      * @throws Exception
85      */
86     @Override
87     public void destroy() throws Exception {
88         SpringContextHolder.clearHolder();
89     }
90 }
复制代码

 本文转自Ryan.Miao博客园博客,原文链接:http://www.cnblogs.com/woshimrf/p/5211191.html,如需转载请自行联系原作者


相关文章
|
29天前
|
缓存 Kubernetes Docker
容器服务ACK常见问题之容器服务ACK ingress websocket配置失败如何解决
容器服务ACK(阿里云容器服务 Kubernetes 版)是阿里云提供的一种托管式Kubernetes服务,帮助用户轻松使用Kubernetes进行应用部署、管理和扩展。本汇总收集了容器服务ACK使用中的常见问题及答案,包括集群管理、应用部署、服务访问、网络配置、存储使用、安全保障等方面,旨在帮助用户快速解决使用过程中遇到的难题,提升容器管理和运维效率。
|
1月前
|
XML Java 开发者
Spring Boot中的bean注入方式和原理
Spring Boot中的bean注入方式和原理
45 0
|
29天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
35 1
|
7天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
14 1
|
23天前
|
敏捷开发 监控 前端开发
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
55 0
|
23天前
|
Java 数据库连接 Spring
|
25天前
|
存储 安全 算法
【C++ 17 包裹类 泛型容器 std::any】深入理解与应用C++ std::any:从泛型编程到多态设计
【C++ 17 包裹类 泛型容器 std::any】深入理解与应用C++ std::any:从泛型编程到多态设计
47 1
|
1月前
|
JSON Java 数据库连接
【spring(五)】SpringMvc总结 SSM整合流程
【spring(五)】SpringMvc总结 SSM整合流程
|
1月前
|
Java 容器 Spring
【spring(一)】核心容器总结
【spring(一)】核心容器总结
|
1月前
|
小程序 前端开发 定位技术
【微信小程序】-- 常用视图容器类组件介绍 -- view、scroll-view和swiper(六)
【微信小程序】-- 常用视图容器类组件介绍 -- view、scroll-view和swiper(六)