获取Spring的ApplicationContext的几种方式
Application Context定义
简单来说就是Spring中的高级容器,可以获取容器中的各种bean组件,注册监听事件,加载资源文件等功能。
具体定义可以参考官网:https://spring.io/understanding/application-context
Understanding Application Context
The ApplicationContext
is the central interface within a Spring application for providing configuration information to the application. It is read-only at run time, but can be reloaded if necessary and supported by the application. A number of classes implement the ApplicationContext
interface, allowing for a variety of configuration options and types of applications.
The ApplicationContext
provides:
Bean factory methods for accessing application components.
The ability to load file resources in a generic fashion.
The ability to publish events to registered listeners.
The ability to resolve messages to support internationalization.
Inheritance from a parent context.
Application Context获取的几种方式
1、直接注入
@Resource
private ApplicationContext ctx;
2、实现ApplicationContextAware接口(推荐)
WebApplicationObjectSupport extends ApplicationObjectSupportimplements ServletContextAware
从上面的继承关系看,获取Application Context还可以继承WebApplicationObjectSupport 、ApplicationObjectSupport,继承加强耦合性不推荐。
3、WebApplicationContextUtils工具类
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
4、从当前线程绑定获取(Spring boot不支持)
ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
最新文章
- javascript序列化和反序列化
- 几个 Ceph 性能优化的新方法和思路(2015 SH Ceph Day 参后感)
- JS替换函数
- 编译 curl with ssl
- 基于Qt实现的截图小程序
- ectouch第七讲 之ECshop模板机制整理
- MongoDB C#驱动中Query几个方法 (转)
- 【M34】如何在同一个程序中结合C++和C
- 动态链接库找不到 : error while loading shared libraries: libgsl.so.0: cannot open shared object file: No such file or directory
- 将yyyyMMdd HH:mm:ss格式的时间转换成时间类型
- Android导入项目时出现红色感叹号
- MySql按指定天数进行分组数据统计分析 2
- 与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态
- struts2教程&;实例
- Ubuntu 16下安装64位谷歌Chrome浏览器
- (MariaDB/MySQL)之DML(2):数据更新、删除
- mongodb怎么创建数据库和配置用户
- Node.js(day4)
- nginx+apache动静分离/负载均衡
- 慕课网c#开发轻松入门6-8最终项目
热门文章
- struts2--上传总结(限制大小和类型 非法上传的跳转)
- 复利计算C语言转java的相关代码
- PHP 操作redis 详细讲解 转的 http://www.cnblogs.com/jackluo/p/3412670.html
- HTTP协议 结构,get post 区别(阿里面试)
- utuntu 安装python3.5
- Spring mvc 数据验证框架注解
- windows200364位iis6 php环境搭建
- Redis windows环境安装 以及 redis整合spring
- BZOJ4950 Wf2017Mission Improbable(二分图匹配)
- Contest 5