使用Spring的ApplicationContextAware接口
在我们的Web应用中,Spring容器通常采用声明式方式配置产生。开发者只要在web.xml中配置一个Listener,该Listener将会负责初始化Spring容器,MVC框架可以直接调用Spring容器中的Bean,无需访问Spring容器本身。在这种情况下,容器中的Bean处于容器管理下,无需主动访问容器,只需接受容器的依赖注入即可。
@Component
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
/*
* (non-Javadoc)
* @see
* org.springframework.context.ApplicationContextAware#setApplicationContext
* (org.springframework.context.ApplicationContext)
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextHolder.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
public static void clearHolder() {
applicationContext = null;
}
}

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
Mac 神兵利器(三) 使用Intellij IDEA打造全栈IDE
前言 作为一个开发者,包括职业与业余,相信大家都在使用集成开发环境IDE。作为专业的开发者,相信大家都是Full Stack Developer,意味着我们的日常开发通常都会涉及多种编程语言比如Java、Python、Shell、Golang、大前端等,通常我们往往要在以下的 “IDE” 列表里选择几种工具搭配使用: Vim、Emacs Sublime、Atom、Visual Studio Eclipse、Jetbrains系列 然而__,__更多的工具选择往往意味着更多的维护及学习成本,所以在这里我推荐大家使用Intellij IDEA,尤其对于Java为主力的全栈工程师及编程初学者,IDEA绝对是首选。 一、安装与配置 1.1 安装方式(MAC为例) 通过官网网站下载 通过 homebrew 安装, brew cask install inte
-
下一篇
Python list, dict, set, tuple
list方法 append: 添加一个新的元素到末尾 extend: 扩展元素 insert: 在任何位置插入元素 pop: 弹出末尾的元素 remove: remove first occurrence of value sort: 原地排序 reverse count clear dict方法 not in clear keys values items get setdefault del a_dict tuple方法 count index set 方法
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合Redis,开启缓存,提高访问速度
- 2048小游戏-低调大师作品
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2全家桶,快速入门学习开发网站教程
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2更换Tomcat为Jetty,小型站点的福音