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 < ...
随机推荐
- A Good Story for Great Friends
There once was a little girl who had a bad temper. Her mother gave her a bag of nails and told her t ...
- [PHP-Debug] 使用 php -l 调试 PHP 错误遇到的坑
有时候,因为系统代码的增加,造成很多文件的相互关联,又或者某些第三接口(微信等),你必须要在线上调试. 线上环境,我们都是设置 “ini_set('display_errors' , false)” ...
- C++互斥器:Mutex
互斥器的功能是,使多个线程和谐工作.同一时间内,只能有一个线程得到互斥对象,并获得资源操作权限,那么如果同一时间其他线程也想去操作资源,此时就会因为Mutex未处于激发状态,而无奈的等待…这时候,线程 ...
- 在Asp.Net MVC中实现上传图片并显示
实现思路大概分为两步: 1. 通过上传接口,将图片上传到服务器,返回文件路径给客户端: 2. 点击保存上传,将文件路径保存到数据库,如果是多张图片,路径用逗号分隔. 核心上传代码: /// <s ...
- C#之asp.net 及MVC 生成动态验证码:
C#之asp.net 及MVC 生成动态验证码: 1.生成验证码字符串 // 随机生成指定长度的验证码字符串private string RandomCode(int length) { string ...
- PHP中日期函数
1,转化为时间戳函数:strtotime() 本函数接受一个包含美国英语日期格式的字符串并尝试将其解析为Unix时间戳,其值相对于now参数给出的时间,如果没有提供此参数则使用系统当前时间. < ...
- web安全之——XSS、CSRF
XSS漏洞 XSS 全称 Cross Site Scripting ,跨站脚本攻击.它的形成主要原因是多数用户输入没有转义,而被直接执行. 参考下面一段脚本: $('#box').html(locat ...
- vsftp小记
安装一个vsftp都有问题(Version: 3.0.2-14ubuntu1),提示530 错误,之后修改配置如下(红色): # cat /etc/pam.d/vsftpdauth required ...
- 【vim】分割窗口、标签页与Quickfix窗口
vim支持窗口分割和标签页,合适地使用这两种特性可以使文字编辑工作更愉快. 1. 窗口分割 vim支持窗口的水平分割和垂直分割.以下是常用的操作指令或快捷键. 命令 说明 vim -o <fil ...
- python-xlwt给excel添加样式
#coding:utf-8import osimport time import xlwt filename="test_xlwt.xls"if os.path.ex ...