SpringMvc获取getbean
2024-08-31 17:43:43
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; /**
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext.
*
*
*/ public class SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class); /**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return applicationContext;
} /**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
assertContextInjected();
return (T) applicationContext.getBean(name);
} /**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> requiredType) {
assertContextInjected();
return applicationContext.getBean(requiredType);
} /**
* 清除SpringContextHolder中的ApplicationContext为Null.
*/
public static void clearHolder() {
logger.debug("清除SpringContextHolder中的ApplicationContext:"
+ applicationContext);
applicationContext = null;
} /**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
// logger.debug("注入ApplicationContext到SpringContextHolder:{}", applicationContext); if (SpringContextHolder.applicationContext != null) {
logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext);
} SpringContextHolder.applicationContext = applicationContext; // NOSONAR
} /**
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
*/
@Override
public void destroy() throws Exception {
SpringContextHolder.clearHolder();
} /**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
}
}
//调用方式
BackPlateService backPlateService = (BackPlateService) SpringContextHolder.getBean("fyNewBackPlateService");
<!-- 获取bean工具注入 -->
//在applicationContext.xml 注入
<bean id="springContextHolder" class="com.eee.common.utils.SpringContextHolder" lazy-init="false"/>
最新文章
- Jquery自定义扩展方法(二)--HTML日历控件
- 通过j-interop访问WMI实例代码
- java中的内部类小结
- 第22章 DLL注入和API拦截(3)
- 杭电1005-Number Sequence
- 用Grunt搭建基于LESS的前端html开发框架
- left (outer) join , right (outer) join, full (outer) join, (inner) join, cross join 区别
- C语言 格式说明符
- NSStringDrawingOptions
- 负载均衡 Lvs nat 模式笔记
- PowerShell:因为在此系统上禁止运行脚本
- Oracle存储过程的调用和执行
- [转]nodejs使用request发送http请求
- ConcurrentHashMap实现原理
- 移动端FastClick和editor冲突问题。
- mac java jdk 安装删除
- print()与println()区别
- 查看CPU序列号以及系统信息
- VirtualBox虚拟机安装ubuntu系统(图文详解)
- JAVA使用qrcode生成二维码(带logo/不带logo)
热门文章
- CSS-position 属性&;元素脱离文档流引发父级边框塌陷问题
- Elasticsearch java api操作(二)(Java High Level Rest Client)
- 风炫安全WEB安全学习第二十五节课 利用XSS键盘记录
- Java并发编程实战(3)- 互斥锁
- 修改postman工具的代码生成工具让它锦上添花
- NOIP初赛篇——08计算机安全知识
- python -c 妙用
- 【Java】计算机软件、博客的重要性、编程语言介绍和发展史
- Windows程序通用自动更新模块(C#,.NET4.5以上)
- sap的内核升级,修补了源代码保护的方式