1、定义一个监听器[MyContextListener],此类里最主要获取springContext和context

  1. package my.request;
  2.  
  3. import javax.servlet.ServletContext;
  4. import javax.servlet.ServletContextEvent;
  5. import javax.servlet.ServletContextListener;
  6.  
  7. import org.springframework.web.context.WebApplicationContext;
  8. import org.springframework.web.context.support.WebApplicationContextUtils;
  9.  
  10. /**
  11. * @author chaox
  12. * @description 通过监听器获取springContext和context,此时普通类只需要提供get和set方法来获取springContext和context
  13. */
  14. public class MyContextListener implements ServletContextListener
  15. {
  16. private ServletContext context = null;
  17.  
  18. // 获取spring注入的bean对象
  19. private WebApplicationContext springContext = null;
  20.  
  21. @Override
  22. public void contextDestroyed(ServletContextEvent event)
  23. {
  24. // Output a simple message to the server's console
  25. System.out.println("The Simple Web App. Has Been Removed");
  26. this.context = null;
  27. }
  28.  
  29. // 这个方法在Web应用服务做好接受请求的时候被调用。
  30. @Override
  31. public void contextInitialized(ServletContextEvent event)
  32. {
  33. this.context = event.getServletContext();
  34.  
  35. if (context == null)
  36. {
  37. System.out.println("获取ServletContext上下文失败!");
  38. return;
  39. }
  40.  
  41. ApplicationContextHelper.setServletContext(context);
  42. // context.setAttribute("xxxKey", "applicationScope");
  43. // context.getAttribute("xxxKey");
  44.  
  45. springContext = WebApplicationContextUtils.getWebApplicationContext(this.context);
  46.  
  47. if (springContext != null)
  48. {
  49. // 获取制定的Sprign的beanId=xxxManager的对象
  50. // xxxManager = (XXXManager) springContext.getBean("xxxManager");
  51. ApplicationContextHelper.setContext(springContext);
  52. }
  53. else
  54. {
  55. System.out.println("获取应用程序上下文失败!");
  56. return;
  57. }
  58.  
  59. //Output a simple message to the server's console
  60. System.out.println("The Simple Web App. Is Ready");
  61. }
  62.  
  63. public ServletContext getContext()
  64. {
  65. return context;
  66. }
  67.  
  68. public void setContext(ServletContext context)
  69. {
  70. this.context = context;
  71. }
  72.  
  73. public WebApplicationContext getSpringContext()
  74. {
  75. return springContext;
  76. }
  77.  
  78. public void setSpringContext(WebApplicationContext springContext)
  79. {
  80. this.springContext = springContext;
  81. }
  82.  
  83. }

2、定义一个普通类[ApplicationContextHelper],此类里最主要提供get和set方法来获取监听器中的springContext和context,从而获取springbean和application范围的对象

  1. package my.request;
  2.  
  3. import javax.servlet.ServletContext;
  4.  
  5. import org.springframework.context.ApplicationContext;
  6.  
  7. public class ApplicationContextHelper
  8. {
  9. private static ApplicationContext context = null;
  10.  
  11. private static ServletContext servletContext = null;
  12.  
  13. public static ApplicationContext getContext()
  14. {
  15. return context;
  16. }
  17.  
  18. public static void setContext(ApplicationContext context)
  19. {
  20. ApplicationContextHelper.context = context;
  21. }
  22.  
  23. public static ServletContext getServletContext()
  24. {
  25. return servletContext;
  26. }
  27.  
  28. public static void setServletContext(ServletContext servletContext)
  29. {
  30. ApplicationContextHelper.servletContext = servletContext;
  31. }
  32. }

3、web.xml配置,需要注意ContextLoaderListener配置的位置需要在自定义的MyContextListener前,否则springContext=null

  1. <listener>
  2. <listener-class>
  3. org.springframework.web.context.ContextLoaderListener
  4. </listener-class>
  5. </listener>
  6.  
  7. <!-- 配置获取springContext和context -->
  8. <listener>
  9. <listener-class>my.request.MyContextListener</listener-class>
  10. </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. Authentication和Authorization的区别

    搞不太清楚Authentication和Authorization的区别,在网上搜了一下,lucky16的一篇文章讲的通俗,看了就懂,记录下来: 你要登机,你需要出示你的身份证和机票,身份证是为了证明 ...

  2. 你真的会写单例模式吗-------Java实现

    转载: 你真的会写单例模式吗--Java实现 单例模式可能是代码最少的模式了,但是少不一定意味着简单,想要用好.用对单例模式,还真得费一番脑筋.本文对Java中常见的单例模式写法做了一个总结,如有错漏 ...

  3. 在内部架设NuGet服务器(转载)

    合肥程序员群:49313181.    合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入)Q  Q:408365330     E-Mail:egojit@qq.com 在公司内部有 ...

  4. 基于双向链表的增删改查和排序(C++实现)

    双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点.一般我们都构造双向循环链表 ...

  5. uva 1001(最短路)

    题意:在一个三维的奶酪里面有n(n<=100)个洞,老鼠A想到达老鼠B的位置,在洞里面可以瞬间移动,在洞外面的移动速度为10秒一个单位,求最短时间 题解:如果两个洞相交,那么d[i][j]=0: ...

  6. IIS7配置asp网站

    An error occurred on the server when processing the URL. Please contact the system administrator. If ...

  7. Hibernate 映射关系

    映射组成关系 •建立域模型和关系数据模型有着不同的出发点: –域模型: 由程序代码组成, 通过细化持久化类的的粒度可提高代码的可重用性, 简化编程 –在没有数据冗余的情况下, 应该尽可能减少表的数目, ...

  8. Linux I2C总线控制器驱动(S3C2440)

    s3c2440的i2c控制器驱动(精简DIY),直接上代码,注释很详细: #include <linux/kernel.h> #include <linux/module.h> ...

  9. 关于MVC

    MVC,或多或少都有听说过.这个模式在客户端程序里面比较常见.以前有人老说mvc是什么设计模式之类.至少我理解的不是.我觉得 MVC是一种模块划分方法.根据它,我们可以快速地划分单独某个模块.比如排行 ...

  10. 使用HTML5+CSS3制作圆角内发光按钮----示例

    <!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...