监听器和普通类获取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下面的文件位置
随机推荐
- STM32上移植ds1307笔记
PS:网上关于ds1307的资料最多还是基于51等单片机的,和stm32上还是略有差别,代码是参考了http://www.openedv.com/posts/list/20167.htm 但是他的代码 ...
- Circular progress bar in Unity 3D
Circular progress bar in Unity 3D - UnityScripthttp://stackoverflow.com/questions/22662706/circular- ...
- 运用js解决java selenium元素定位问题
一.解决定位并操作uneditable元素 尝试了通过id,xpath等等定位元素后点击都提示Element is not clickable at point 再看了下可以click的元素发现上面有 ...
- Yii 检查输入的数据是否已经存在
只需要在对应的Model文件中的rules方法里面加入 array('username', 'unique', 'message'=>'{attribute}已存在'),//username是需 ...
- 介绍开源的.net通信框架NetworkComms框架之六 x509证书通信
原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 目前作者已经开源 许可是 ...
- 当BPM业务流程管理遇上制造业
2015年5月,K2正式与赛科利签约,准备上线核心采购类流程,包括PR.PO.Payment.供应商管理等. 上海赛科利汽车模具技术应用有限公司隶属于上汽集团,现有员工2300余人.为解决汽车“安全” ...
- hdu 1041 (OO approach, private constructor to prevent instantiation, sprintf) 分类: hdoj 2015-06-17 15:57 25人阅读 评论(0) 收藏
a problem where OO seems more natural to me, implementing a utility class not instantiable. how to p ...
- sql数据库 管理处理问题--维护计划
问题:SQLServer 错误: 15404,无法获取有关 Windows NT 组/用户 MYPC/Administrator' 的信息,错误代码 0x534. [SQLSTATE 42000] ( ...
- jQueryMobile控件之ListView
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java的内部类
Java的内部类 首先我们来了解一下什么是内部类? 内部类是指在一个外部类的内部再定义一个类.内部类可以是静态static的,也可用public,default,protected和private修饰 ...