监听器和普通类获取springContext和context,从而获取springbean和application范围的对象
1、定义一个监听器[MyContextListener],此类里最主要获取springContext和context
package my.request; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; /**
* @author chaox
* @description 通过监听器获取springContext和context,此时普通类只需要提供get和set方法来获取springContext和context
*/
public class MyContextListener implements ServletContextListener
{
private ServletContext context = null; // 获取spring注入的bean对象
private WebApplicationContext springContext = null; @Override
public void contextDestroyed(ServletContextEvent event)
{
// Output a simple message to the server's console
System.out.println("The Simple Web App. Has Been Removed");
this.context = null;
} // 这个方法在Web应用服务做好接受请求的时候被调用。
@Override
public void contextInitialized(ServletContextEvent event)
{
this.context = event.getServletContext(); if (context == null)
{
System.out.println("获取ServletContext上下文失败!");
return;
} ApplicationContextHelper.setServletContext(context);
// context.setAttribute("xxxKey", "applicationScope");
// context.getAttribute("xxxKey"); springContext = WebApplicationContextUtils.getWebApplicationContext(this.context); if (springContext != null)
{
// 获取制定的Sprign的beanId=xxxManager的对象
// xxxManager = (XXXManager) springContext.getBean("xxxManager");
ApplicationContextHelper.setContext(springContext);
}
else
{
System.out.println("获取应用程序上下文失败!");
return;
} //Output a simple message to the server's console
System.out.println("The Simple Web App. Is Ready");
} public ServletContext getContext()
{
return context;
} public void setContext(ServletContext context)
{
this.context = context;
} public WebApplicationContext getSpringContext()
{
return springContext;
} public void setSpringContext(WebApplicationContext springContext)
{
this.springContext = springContext;
} }
2、定义一个普通类[ApplicationContextHelper],此类里最主要提供get和set方法来获取监听器中的springContext和context,从而获取springbean和application范围的对象
package my.request; import javax.servlet.ServletContext; import org.springframework.context.ApplicationContext; public class ApplicationContextHelper
{
private static ApplicationContext context = null; private static ServletContext servletContext = null; public static ApplicationContext getContext()
{
return context;
} public static void setContext(ApplicationContext context)
{
ApplicationContextHelper.context = context;
} public static ServletContext getServletContext()
{
return servletContext;
} public static void setServletContext(ServletContext servletContext)
{
ApplicationContextHelper.servletContext = servletContext;
}
}
3、web.xml配置,需要注意ContextLoaderListener配置的位置需要在自定义的MyContextListener前,否则springContext=null
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <!-- 配置获取springContext和context -->
<listener>
<listener-class>my.request.MyContextListener</listener-class>
</listener>
监听器和普通类获取springContext和context,从而获取springbean和application范围的对象的更多相关文章
- Android开发中Context类的作用以及Context的详细用法
Android中Context的作用以及Context的详细用法 本文我们一起来探讨一下关于Android中Context的作用以及Context的详细用法,这对我们学习Android的资源访问有很大 ...
- Spring获取bean工具类,可用于在线程里面获取bean
Spring获取bean工具类,可用于在线程里面获取bean import java.util.Locale; import org.springframework.beans.BeansExcept ...
- 在任意位置获取应用程序CONTEXT
Android程序中访问资源时需要提供Context,一般来说只有在各种component中(Activity, Provider等等)才能方便的使用api来获取Context, 而在某些工具类中要获 ...
- Context.managedQuery()和context.getContentResolver()获取Cursor关闭注意事项
在获取图片缩略图时,获取游标并进行相关的操作. Cursor cursor = context.getContentResolver().query(MediaStore.Images.Thumbna ...
- 错误:找不到类org.springframework.web.context.ContextLoaderListener
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...
- Fragment中获取Activity的Context
Fragment中获取Activity的Context时只需要this.getActivity()即可.
- 玩下软工项目,第一轮--全局Context的获取,SQLite的建立与增删改查,读取用户通话记录信息
项目的Github地址:https://github.com/ggrcwxh/LastTime 采用基于git的多人协作开发模式 软件采用mvc设计模式,前端这么艺术的事我不太懂,交给斌豪同学去头疼了 ...
- C# 有关控件、自定义类事件中的委托链的获取、移除操作
直接来代码吧,这样干脆直接,也不耽误我午休了.一切尽在源码中. public class ControlEventTool { /// <summary> /// 移除控件的某类事件, 如 ...
- context.getResourceAsStream获取的是部署在服务器上面的文件位置 而不是我们本地的工程位置 意思是说获取的都是web下面的文件位置
context.getResourceAsStream获取的是部署在服务器上面的文件位置 而不是我们本地的工程位置 意思是说获取的都是web下面的文件位置
随机推荐
- centos 安装 mongdb
1.安装MongoDB(安装到/usr/local) wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.2.4.t ...
- 关于phpstorm中安装配置xdeug
最近从网上找了好多phpstorm中配置安装xdebug的信息,但是貌似都失败了 ...我也不知道是为什么... 突然有一天 不知道怎么整的就配置成功了 现在可以分享一下了 正好我用的软件的版本 ...
- iOS基础篇(十三)——UITableView(一)重用机制
UITableView是app开发中常用到的控件,功能很强大,常用于数据的显示.在学习UITableView使用之前,我们先简单了解一下: 1.UITableView的重用机制 UITableView ...
- iOS开发网络篇—搭建本地服务器
iOS开发网络篇—搭建本地服务器 一.简单说明 说明:提前下载好相关软件,且安装目录最好安装在全英文路径下.如果路径有中文名,那么可能会出现一些莫名其妙的问题. 提示:提前准备好的软件 apache- ...
- iOS开发网络篇—NSURLConnection基本使用
iOS开发网络篇—NSURLConnection基本使用 一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据 ...
- Python、PIP环境变量的配置
Python安装的路径:D:\Python35 pip的环境变量 Python和pip的PATH: PIP下载链接:https://pypi.python.org/pypi/pip 随意解压好,然后C ...
- 自定义控件之 TextBox
//textbox typevar boxType = { WaterMarkBox: 0, ValidateBox: 1, SearchBox: 2}var textBoxObj = functio ...
- HDU 4939 Stupid Tower Defense (2014 Multi-University Training Contest 7)
思路:首先红色肯定要放在最后面.前面蓝色和绿色dp求解. dp[i][j] 表示前面(i+j) 个 有 i 个蓝色塔 j个绿色塔 能造成最大伤害. //====================== ...
- Spring与JPA
Java持久化API(Java Persistence API),即JPA Spring中使用JPA的第一步是要在Spring应用上下文中将实体管理器工厂(entity manager factory ...
- 滤镜与CSS3效果
-webkit-filter是css3的一个属性,Webkit率先支持了这几个功能,感觉效果很不错.一起学习一下filter这个属性吧. 现在规范中支持的效果有: grayscale 灰度 ...