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 < ...
随机推荐
- 搜狗Q3业绩迅猛增长,战略整合稳步推进
继9月16日腾讯与搜狗战略结盟之后,最近搜狗再次吸引了业界关注的目光,10月29日,搜狗公布了截至2013年9月30日的第三季度未经审计的财务报告.财报显示,新搜狗Q3营收达5700万美元,同 ...
- 阿里巴巴集团2013实习生招聘技术类笔试卷(B)
一.单选题 1.在常用的网络协议中,___是面向连接的.有重传功能的协议. A.IP B.TCP C.UDP D.DXP 2.500张多米诺骨牌整齐地排成一列,依顺序编号为1.2.3… ...
- 转载:R语言Data Frame数据框常用操作
Data Frame一般被翻译为数据框,感觉就像是R中的表,由行和列组成,与Matrix不同的是,每个列可以是不同的数据类型,而Matrix是必须相同的. Data Frame每一列有列名,每一行也可 ...
- AbpZero之企业微信---登录(拓展第三方auth授权登录)---第三步:需要注意事项
1.AbpZero的auth登录会在数据库中的AbpUserLogins表会根据你登录的ProviderKey和Provider来生成生成一条记录,ProviderKey在表中是唯一的: 2.要登录成 ...
- Abp mvc angular 添加视图
在LawAndRegulation项目中添加导航路由(Abp添加菜单)对应的客户端页面. 创建文件 客户端页面在Abp模板项目中默认存放在Abp/Main/views文件夹下,在项目中我们创建属于字典 ...
- webform获取微信用户的授权
这是一个利用webform做出来的简单demo,微信授权,获取微信用户的基本信息.方便以后加深记忆. public partial class Index : System.Web.UI.Page { ...
- dorado7-HelloWorld
1.首先在Tomat中将 Auto reloding enable去掉,去掉的目的不用每次更改代码,都要重新部署 2.创建dorado视图文件 2.1 视图文件的格式为xml 2.2 在view中添加 ...
- 解决:百度编辑器UEditor,怎么将图片保存到图片服务器,或者上传到ftp服务器的问题(如果你正在用UE,这篇文章值得你看下)
在使用百度编辑器ueditor的时候,怎么将图片保存到另一个服务器,或者上传到ftp服务器?这个问题,估计很多使用UE的人会遇到.而且我百度过,没有找到这个问题的解决方案.那么:本篇文章就很适合你了. ...
- Windows10 家庭版添加【本地组策略编辑器】
Windows10 家庭版默认没有[本地组策略编辑器],添加方法: 新建记事本复制以下内容 @echo off pushd "%~dp0" dir /b C:\Windows\se ...
- Ruby for Sketchup 贪吃蛇演示源码(naive_snake)
sketchup是非常简单易用的三维建模软件,可以利用ruby 做二次开发, api文档 http://www.rbc321.cn/api 今天在su中做了一款小游戏 贪吃蛇,说一下步骤 展示 主要思 ...