监听器和普通类获取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下面的文件位置
随机推荐
- 超越线程池:Java并发并没有你想的那么糟糕
转载: 超越线程池:Java并发并没有你想的那么糟糕
- Django URL传递参数的方法总结(转)
1 无参数情况 配置URL及其视图如下: 1 2 3 4 (r'^hello/$', hello) def hello(request): return HttpResponse("He ...
- break和continue的区别
break是结束整个循环体,continue是结束单次循环
- Yii2框架安装(windows)
-->安装PHP环境Wamp集成环境,XAMMP等.-->安装Composerhttp://pan.baidu.com/s/1i3fejjvPS:安装过程中的有一个手动操作项选择php.e ...
- 【转载】JSP常用跳转方式
转自:http://blog.csdn.net/wanghuan203/article/details/8836326 (1)href超链接标记,属于客户端跳转 (2)使用javascript完成,属 ...
- Objective-C( block的使用)
block block用来保存一段代码 block的标志:^ block跟函数很像:可以保存代码.有返回值.有形参.调用方式跟调用方法一样 block内部可以访问外面的变量 默认情况下,block内部 ...
- PHP投票系统
1.投票页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- 练习使用XRecyclerView,可上拉下拉刷新。
package com.lixu.testxrecyclerview; import android.support.v7.app.AppCompatActivity; import android. ...
- Java 导出EXCEL
1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...
- windowsAzure模拟器错误
Failed to initialize Microsoft Azure storage emulator. Unable to open wait handle 无法初始化微软Azure存储模拟器. ...