监听器和普通类获取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下面的文件位置
随机推荐
- Sqlserver2012 中文乱码解决
1.在Windows Azure的数据库中,如果选择默认字符编码,那么在创建表字段是,字符串类型应该为nvarchar,如果是varchar将会出现乱码,同样的的在sql语句中生命变量,也是需要将字符 ...
- UNIX 逐字符输入
//终端驱动处于普通胡一次一字符模式 system("stty raw"); //终端驱动处于普通胡一次一行模式 system("stty cooked");
- ModalPopup
原文地址:http://ajax.asp.net/ajaxtoolkit/ModalPopup/ModalPopup.aspx ModalPopup 描述 ModalPopup 能够使页面以设计对话框 ...
- a标签中的点击事件
我们常用的在a标签中有点击事件:1. a href="javascript:js_method();" 这 是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问 ...
- ssm(spring,springmvc,mybatis)
1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...
- 最新为Phpstorm配置xdebug 进行断点调试
额 ,曾经写过一个,现在发现不咋好使了 ,你说咋整,下载xdebug的时候 还得注意系统是32位还是64位,而且一堆下载文件不知道是哪个. 额,所以我现在发现有个更好的方法,啥也不下了 直接配把 ...
- Props属性
大多数组件在创建时就可以使用各种参数来进行定制.用于定制的这些参数就称为props(属性). import React, { Component } from 'react'; import { Ap ...
- .htaccess设置自定义出错页面
404错误可以这么写 ErrorDocument code error.php 如果是404错误,跳到文件error.php 其他常用错误页面写法(其中404错误有2种写法,上面一种,下面是通用错误定 ...
- redis教程
windows下安装redis: http://jingyan.baidu.com/article/49ad8bce40174f5834d8fa24.html redis教程: http://www. ...
- Solr安装入门、查询详解
Solr安装入门:http://www.importnew.com/12607.html 查询详解:http://www.360doc.com/content/14/0306/18/203871_35 ...