获取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();
获取Spring的ApplicationContext的几种方式的更多相关文章
- Java代码获取spring 容器的bean几种方式
一.目的 写了一个项目,多个module,然后想在A模块中实现固定的config注入,当B模块引用A时候,能够直接填写相对应的配置信息就行了.但是遇到一个问题,B引用A时候,A的配置信息总是填充不了, ...
- 获取spring的ApplicationContext几种方式【转】
转自:http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html Java类获取spring 容器的bean 常用的5种获取spring 中bean的方式 ...
- Spring创建JobDetail的两种方式
一.Spring创建JobDetail的两种方式 二.整合方式一示例步骤 1.将spring核心jar包.quartz.jar和Spring-context-support.jar导入类路径. 2.编 ...
- Spring 循环依赖的三种方式(三级缓存解决Set循环依赖问题)
本篇文章解决以下问题: [1] . Spring循环依赖指的是什么? [2] . Spring能解决哪种情况的循环依赖?不能解决哪种情况? [3] . Spring能解决的循环依赖原理(三级缓存) 一 ...
- spring配置属性的两种方式
spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...
- Spring实现AOP的4种方式(转)
转自:http://blog.csdn.net/udbnny/article/details/5870076 Spring实现AOP的4种方式 先了解AOP的相关术语:1.通知(Advice):通知定 ...
- Spring 使用AspectJ的三种方式
Spring 使用AspectJ 的三种方式 一,使用JavaConfig 二,使用注解隐式配置 三,使用XML 配置 背景知识: 注意 使用AspectJ 的 时候 要导入相应的Jar 包 嗯 昨天 ...
- 运行 Spring Boot 应用的 3 种方式
今天介绍 3 种运行 Spring Boot 应用的方式,看大家用过几种? 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spring Boot 开启的 ...
- Spring事务管理的四种方式(以银行转账为例)
Spring事务管理的四种方式(以银行转账为例) 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败. 原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不 ...
随机推荐
- Java GUI 点击按钮退出
import java.awt.*; import java.awt.event.*; public class TestFrameTwo implements ActionListener { Fr ...
- 配置resin web方式部署项目
写在前面,推荐下载resin4.0.47版本.其它版本没有测试 最近打算做一个小项目,然后容器选用了resin.想通过web提交war文件的方式 进行部署,更新代码也方便. 试了resin最新的版本( ...
- [并查集] More is Better
题目描述 Mr Wang wants some boys to help him with a project. Because the project is rather complex, the ...
- Mac10.11.2 Apache 服务配置
系统默认是隐藏apache安装目录的,但我们可以通过“命令行”或者“文件夹前往”的方式找到它.它是安装在系统的私有目录下,也就是/private/etc下面,因为它是隐藏的,所以我们无法通过界面找到它 ...
- BurpSuite 激活破解
1.下载软件关于Burp Suite, 它是进行Web应用安全测试的一个集成平台,无缝融合各种安全工具并提供全面的接口适配,支持完整的Web应用测试流程,从最初的映射和应用程序的攻击面分析到发现和利用 ...
- 探究Android中通过继承ViewGroup自定义控件的原理
原文地址:http://www.cnblogs.com/kross/p/3378395.html 今天断断续续的折腾了一下午到现在20:38,终于有点明白了.o(╯□╰)o 在Android开发中,我 ...
- HDU4675_GCD of Sequence
很有意思的一个数论题. 是这样的,给你一个数数组a[i],其中给出的每一个数字和要求的数字方位都是[1,m],现在问你根据a[]构造一个b[],且a和b中间的不相等的元素的个数恰好有k个. 现在问你g ...
- 【数据库_Postgresql】sql查询结果添加序号列
ROW_NUMBER () OVER (ORDER BY A .ordernumber ASC) AS 序号
- 【JavaScript&jQuery】返回顶部
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- QT 主窗口和子窗口相互切换示例
QT 主窗口和子窗口相互切换示例 文件列表: SubWidget.h #ifndef SUBWIDGET_H #define SUBWIDGET_H #include <QtWidgets/QW ...