spring中获取applicationContext(2)
前几天写web项目的时候,用到了spring mvc。
但是又写bean。我要在代码里面生成,而这个bean里面,又有一些属性是通过spring注入的。
所以,只能通过ApplicationContext来获取。
在servlet里面获取ApplicationContext其实可以通过spring提供的方法:
|
1
|
WebApplicationContextUtils.getWebApplicationContext(ServletContext) |
来获取。
这个方法前提是要在web.xml里面即一个listener:
|
1
2
3
4
5
6
7
8
9
10
|
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-servlet.xml, classpath:applicationContext.xml </param-value> </context-param><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> |
不过这样子只能在servlet里面获取。
如果要在servlet外获取ApplicationContext呢?
其实可以封装一下的:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package me.idashu.code.util;import org.springframework.context.ApplicationContext;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;/** * 描述:ApplicationContext容器 * * @author: dashu * @since: 13-5-11 */public class AppContext implements ServletContextListener { private static WebApplicationContext springContext; public AppContext() { super(); } public void contextInitialized(ServletContextEvent event) { springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()); } public void contextDestroyed(ServletContextEvent event) { } public static ApplicationContext getApplicationContext() { return springContext; }} |
还需要在web.xml里面再加条记录:
|
1
2
3
|
<listener> <listener-class>me.idashu.code.util.AppContext</listener-class> </listener> |
其实就是在content初始化的时候,把ApplicationContext保存起来,下次方便调用。
spring中获取applicationContext(2)的更多相关文章
- spring中获取applicationContext
常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXm ...
- spring中获取ApplicationContext对象的技巧,含源码说明
第一步,实现接口ApplicationContextAware,重写setApplicationContext方法,下方代码标红的地方,绿色部分 可以通过声明来进行存储到本类中. @Component ...
- spring mvc在Controller中获取ApplicationContext
spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...
- spring中获取dao或对象中方法的实例化对象
spring中获取dao的中方法的实例化对象: //获取应用上下文对象 ApplicationContext ctx = new ClassPathXmlApplicationContext(&quo ...
- Spring中获取被代理的对象
目录 Spring中获取被代理的对象 获取Spring被代理对象什么时候可能会用到? Spring中获取被代理的对象 Spring中获取被代理的对象 ### 获取Spring被代理对象的JAVA工具类 ...
- spring中获取当前项目的真实路径
总结: 方法1: WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext ...
- spring 代码中获取ApplicationContext(@AutoWired,ApplicationListener)
2017年度全网原创IT博主评选活动投票:http://www.itbang.me/goVote/234 学习spring框架时间不长,一点一滴都得亲力亲为.今天忽然觉得老是通过@Autowir ...
- Spring中获取对象
Spring是一个非常主流,而且是好用的框架.提供管理对象的容器,提供事务的支持,缓存,权限认证(往往不用).用来集成hibernate等.而管理对象的生命周期是其中一个非常重要的功能.在Spring ...
- Spring +quartz获取ApplicationContext上下文
job存在数据库中,能够进行动态的增增删改查,近期遇到了怎样获取ApplicationContext上下文的问题.解决的方法例如以下 applicationContext-quartz.xml < ...
随机推荐
- fully delete project in Eclipse
选择你的项目(test)右击,选择delete——弹出框中勾选删除全部,如下如所示: 正常情况下,这样就能删除干净了,有时候你项目在运行,这时候你点击删除,那就会报下面的错误提示,虽然不会影响你其它项 ...
- EBS Archiving and Purging: You Know you need to
A number of trends in the IT industry have contributed to the increasing size of ERP application dat ...
- oracle 导出导入命令
imp YG_XSOA_NEW/kingo@20.14.12.14/XSSJZX file=d:\daochu.dmp full=y (导入) exp YG_XSOA_NEW/kingo@20 ...
- SignalR 设计理念(二)
SignalR 设计理念(二) 实现客户端和服务器端的实时通讯. 前言: 客户端方法忽略大小写,主要原因基于是URL对大小写不敏感的问题,开发者之间为了更好的协同开发,定下的开发者协议. 问题阐述 客 ...
- electron-vue 开发环境搭建(Windows环境)
1.安装 Node.js,一切都基于这个.安装完成后,终端输入 node -v 验证. 2.建立自己的项目目录,然后进入目录. 安装 vue-cli ,终端输入 npm install -g vue- ...
- 使用xargs与awk联合使用批量杀进程,很方便
ps -ef | grep java | grep alarm | awk '{print $2}' | xargs kill -9 注*A. $2表示第2列,即进程号PID; awk很强大,这里不 ...
- 1047 行 MySQL 详细学习笔记
https://blog.csdn.net/baidu_25310663/article/details/86517610 Windows服务 -- 启动MySQL net start mys ...
- C++一些函数的意义
1.重载 : C++ 允许多个函数拥有相同的名字,只要它们的参数列表不同就可以,这就是函数的重载 2.隐藏和覆盖的区别 IF 子类的函数与父类的名称相同,但是参数不同 父类函数被隐藏(还存在) ELS ...
- 使用Navicat 创建mysql存储过程,实现日期加流水号序列
目的:使用Navicat 创建mysql存储过程,实现格式为8位日期(年月日)+5位流水号序列. 步骤: 1.打开Navicat 登录数据库,点击导航栏上的函数,如下图: 2.点击新建函数,选择“过程 ...
- FunDA(11)- 数据库操作的并行运算:Parallel data processing
FunDA最重要的设计目标之一就是能够实现数据库操作的并行运算.我们先重温一下fs2是如何实现并行运算的.我们用interleave.merge.either这几种方式来同时处理两个Stream里的元 ...