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范围的对象的更多相关文章

  1. Android开发中Context类的作用以及Context的详细用法

    Android中Context的作用以及Context的详细用法 本文我们一起来探讨一下关于Android中Context的作用以及Context的详细用法,这对我们学习Android的资源访问有很大 ...

  2. Spring获取bean工具类,可用于在线程里面获取bean

    Spring获取bean工具类,可用于在线程里面获取bean import java.util.Locale; import org.springframework.beans.BeansExcept ...

  3. 在任意位置获取应用程序CONTEXT

    Android程序中访问资源时需要提供Context,一般来说只有在各种component中(Activity, Provider等等)才能方便的使用api来获取Context, 而在某些工具类中要获 ...

  4. Context.managedQuery()和context.getContentResolver()获取Cursor关闭注意事项

    在获取图片缩略图时,获取游标并进行相关的操作. Cursor cursor = context.getContentResolver().query(MediaStore.Images.Thumbna ...

  5. 错误:找不到类org.springframework.web.context.ContextLoaderListener

    严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...

  6. Fragment中获取Activity的Context

    Fragment中获取Activity的Context时只需要this.getActivity()即可.

  7. 玩下软工项目,第一轮--全局Context的获取,SQLite的建立与增删改查,读取用户通话记录信息

    项目的Github地址:https://github.com/ggrcwxh/LastTime 采用基于git的多人协作开发模式 软件采用mvc设计模式,前端这么艺术的事我不太懂,交给斌豪同学去头疼了 ...

  8. C# 有关控件、自定义类事件中的委托链的获取、移除操作

    直接来代码吧,这样干脆直接,也不耽误我午休了.一切尽在源码中. public class ControlEventTool { /// <summary> /// 移除控件的某类事件, 如 ...

  9. context.getResourceAsStream获取的是部署在服务器上面的文件位置 而不是我们本地的工程位置 意思是说获取的都是web下面的文件位置

    context.getResourceAsStream获取的是部署在服务器上面的文件位置 而不是我们本地的工程位置 意思是说获取的都是web下面的文件位置

随机推荐

  1. spring+mongo

    一.程序结构

  2. 用sqlplus为oracle创建用户和表空间<转>

    用Oracle10g自带的企业管理器或PL/SQL图形化的方法创建表空间和用户以及分配权限是相对比较简单的,本文要介绍的是另一种方法,使用Oracle 9i所带的命令行工具:SQLPLUS来创建表空间 ...

  3. 能源项目xml文件 -- springMVC-servlet.xml -- context:component-scan

    <context:component-scan base-package="com.xindatai.ibs" use-default-filters="false ...

  4. css学习笔记 1

    对于一个页面,如何控制页面的结构就看如何去理解css的各个属性了,只有了解了css的各个属性后才能更有效的让css控制页面的任何一个结构. css的结构:选择符:{属性名1:属性值; 属性名2:属性值 ...

  5. linux 下mysql的安装,并设置必要的密码

    首先,我使用的是redhat linux ,版本号为: [root@localhost init.d]# cat /proc/version Linux version - (bhcompile@po ...

  6. 如何在MapControl界面添加双击事件实现标绘及符号样式更改

    private void axMapControl1_OnDoubleClick(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnDo ...

  7. 关于ecshop的那些故事

    1.php下foreach()错误提示Warning: Invalid argument supplied for foreach() 错误提示:Warning: Invalid argument s ...

  8. C# 自动运行代码 (创建windows 服务的形式 )

    本文转载自:http://blog.csdn.net/csethcrm/article/details/17917721 1.      新建项目 1.1 右键解决方案 – 添加 – 新建项目 1.2 ...

  9. maven设置

    <localRepository>D:/apps/eclipse/env/maven/repository</localRepository>

  10. session没有过期,其保存的数据无故丢失的原因

    问题: 我们经常会做到的一个功能,将登录用户信息保存到session中,在页面上显示登录用户名.但是,如果很短时间内甚至几秒没有刷新这个页面的话,这个用户名就消失了,其实就是session中保存的用户 ...