spring bean的获取方式
来源:互联网 发布:澳洲传媒硕士 知乎 编辑:程序博客网 时间:2024/05/22 09:00
private static Demo demo = ((Demo)SpringContextHolder.getBean("demo"));/** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext. * */@Service@Lazy(false)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() { if (logger.isDebugEnabled()){ logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext); } applicationContext = null; } /** * 实现ApplicationContextAware接口, 注入Context到静态变量中. */ @Override public void setApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext = applicationContext; } /** * 实现DisposableBean接口, 在Context关闭时清理静态变量. */ @Override public void destroy() throws Exception { SpringContextHolder.clearHolder(); } /** * 检查ApplicationContext不为空. */ private static void assertContextInjected() { Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder."); }}
阅读全文
0 0
- spring 获取bean的方式
- spring bean的获取方式
- Spring获取Bean的几种方式
- Spring获取Bean的几种方式
- Spring获取Bean的几种方式
- Spring获取Bean的几种方式
- Spring获取Bean的几种方式
- Spring获取Bean的几种方式
- Spring中获取bean的方式总结
- spring中bean的获取方式总结
- Spring中获取bean的方式总结
- spring获取bean的几种方式
- Spring获取bean的三种方式
- spring 获取bean的几种方式
- spring 获取Bean的几种方式
- Spring获取bean的几种方式
- Spring中获取bean的方式
- spring通过编程的方式获取bean
- CentOS7.0下安装Apache2.4.20版本,安装完成之后,报 You don't have permission to access / on this server.
- 找不org.springframework.boot.context.embedded.FilterRegistrationBean
- 身份证校验 日期 长度格式 表达式
- list集合通过ajax 在页面显示 、 以及省市级联查询
- Error:Flavor 'prod' has no flavor dimension
- spring bean的获取方式
- OpenGL入门教程(2)
- 借由ARM CORTEX-M芯片分析C程序加载和存储模型
- 如何让li横排显示
- Spark性能调优:使用Kryo序列化库、RDD的压缩
- java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect to this MySQ
- ansible基础入门
- unbuntu运行gcc出现collect2: fatal error: cannot find 'ld'
- display:flex 布局教程 (赞,清晰)