作者:江南白衣

Spring再强大,也要面对降临的问题--因为Spring不是Weblogic、Tomcat般的顶层容器,Servlet和EJB对象不由它创建,所以它必须要降临到Weblogic、Tomcat所在的位面。
     初学者一般不用管那么多,照着Spring+hibernate+Struts之类的Sample就做了,但慢慢的,也许就要开始在jsp+javabean体系,土制框架,singleton类等环境下使用Spring了。
     《Professional Java Development with the Spring Framework》第3章有"Managing the Containe"一节讲这个问题。一般可以分为直接召唤系与IoC fashion两类。

1.直接召唤系--Singleton的Application Context
      最简单的,就像在UnitTest里那样,直接构造Application Context:

ApplicationContext ctx = new ClasspathXmlApplicationContext("ApplicationContext.xml");

在Web环境里,会使用ContextLoader构造ApplicationContext后,压进Servlet Context。
      由ContextLoaderListener或ContextLoaderServlet,在Web应用启动时完成。
      然后在Jsp/Servelet中,可以通过Servlet Context取得ApplicationContext:

ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application);

但像singleton类或者EJB中,就没有Servlet Context可用了。
      如果全部像UnitTest那样直接构造,速度就会很不堪。自然的,就想到把ApplicationContext做成单例。 
      Spring提供了ContextSingletonBeanFactoryLocator这样的物体。
      先搞一个beanRefFactory.xml,里面写上所有的applcationContext-*.xml文件名,并把Context命名为"default-context":

<beans>
  <bean id="default-context"  class="org.springframework.context.support.ClassPathXmlApplicationContext">
    <constructor-arg>
      <list> <value>applicationContext.xml</value></list>
    </constructor-arg>
  </bean>
</beans>

然后让loactor去找它,但代码有点长:

BeanFactoryReference bfr =  DefaultLocatorFactory.getInstance().useBeanFactory("default-context");
BeanFactory factory = bfr.getFactory();
MyService myService = factory.getBean("myService");
bfr.release();
// now use myService

上面的代码实在是太灵活,太麻烦了。
    还不如自己实现一个简单的Singleton,扩展ContextLoaderListener类,在Web系统启动时压入Singleton。

新的ContextLoaderListener类重载如下,ContextUtil中包含一个静态的ApplicationContext变量:

    public void contextInitialized(ServletContextEvent event)
    {
        super.contextInitialized(event);

ServletContext context = event.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        ContextUtil.setContext(ctx);
    }

用家可直接取用:

 ApplicationContext context = ContextUtil.getContext();

2.IoC fashion
    如果所有地方都使用直接召唤系,那就反而是在打Rod的耳光了。因为他一直都反对代码与框架深耦合的。
    所以,更好的方法是写一些glue code、base class来完成Spring的降临,而不让应用代码察觉Spring Application Context的存在。
    不过,因为各个框架的结构不同,Rod也没办法讲出一个通用的整合方法,所以建议大家尽量学习已整合的各种框架,如Spring MVC、Struts的种种方式,写出自己的简单整合代码来。

只有不确定的调用某些Singleton类,不适合过早ioc的情况,可以使用直接召唤系。

转载于:https://www.cnblogs.com/wangbinjr/archive/2010/06/11/2354198.html

Spring的春天,直接召唤系--Singleton的Application Context相关推荐

  1. idae中spring mvc解决问题application context not configured for this file于spring框架使用中的原因

    idae中spring mvc解决问题application context not configured for this file于spring框架使用中的原因 参考文章: (1)idae中spr ...

  2. spring boot实战(第六篇)加载application资源文件源码分析

    前言 在上一篇中了解了spring配置资源的加载过程,本篇在此基础上学习spring boot如何默认加载application.xml等文件信息的. ConfigFileApplicationLis ...

  3. Spring application context resource is not injected.

    在加载数据的时候,报错 但是再run一次的时候,错误又消失了 [2019-04-18T16:47:46,396][INFO ][main][GridDiscoveryManager] Topology ...

  4. Spring Boot整合Nacos时遇到 java.lang.IllegalStateException: Context has been already given a name 的解决办法

    问题概述 在进行项目开发过程中,通过Spring Boot整合Alibaba Nacos的服务注册与发现和配置中心时,遇到个梗,启动时打印出一串错误日志信息 " Failed to rena ...

  5. spring(春天)

    目录 spring案例 spring优点 组成 引入基础jar包 IOC理论 IOC容器--Bean管理xml的方式 什么是bean管理? IOC操作Bean管理两种方式(创建类和注入属性的方式) I ...

  6. spring中的Bean的作用域-singleton

    Spring提供了五中scope.分别是 singleton.prototype.request.session.global session. 我们主要讨论前两者的区别? 当一个bean被声明为单例 ...

  7. Spring Cloud入门系列(1)- Spring生态体系发展史+全系框架介绍

    Spring发展史 2000年,Java EE和EJB迅速发展,很多知名公司都是采用此技术方案进行项目开发,但是EJB 属于重量级框架,开发繁琐.于是一个叫Rod Johnson的大佬写了一本叫做&l ...

  8. 学习SPRING在春天 - Learn Spring in spring(二)

    二.spring管理对象的简单例子 任何需要交给spring管理的对象,都必须在配置文件中注册,这个过程被称为wiring,下面做一个最简单的Hello world演示,我们将要注册的类如下: /* ...

  9. Bug整理——Spring boot 执行Junit Test时不加载Application中的参数的问题

    在我们的项目中,需要在Application中设置几个系统参数才能正常打印日志,所以在Application.java中,我们是这么写的: @EnableTransactionManagement @ ...

最新文章

  1. Oracle可以处理LOB字段的常用字符函数
  2. 1.2.2 认识各个硬件部件
  3. Android中使用Notification在通知栏中显示通知
  4. MongoDB PHP数据库查询类
  5. python string模块template_Python标准库笔记(1) — string模块
  6. maven库的查询和配置
  7. 基于大数据可视化技术的毕业生就业分析服务项目 (软件创新设计期末报告)
  8. 超星的Pdg/Pdz转带书签目录Pdf的好方法
  9. 简历是html的怎么改成word文档,word简历使用教程.doc
  10. 使用PMT和IPMT在Excel中显示贷款付款
  11. VC编程——QQ连连看外挂
  12. java虚拟内存不足
  13. 类型查找器 ITypeFinder
  14. 名侦探柯南之零的执行人
  15. android svg 线条动画教程,简单的SVG线条动画
  16. 11月更新M1芯片专用PS photoshop 2021/2022安装 PS2021/22 Mac版 附详细教程
  17. 控制window系统应用程序定时关闭和开启
  18. Brain Stimulation: ​大脑电生理记录和刺激工具包(BEST)
  19. 使用python识别图像中的文字
  20. 本安型变送器电池701PGNKF

热门文章

  1. 我参与的一个项目的继续总结:牢骚篇
  2. kafka jar包_和同事交流不会kafka怎么行,API奉上,不是大神也能编
  3. 95-910-165-源码-FlinkSQL-Flink SQL 中的时间属性
  4. 【hadoop】Hadoop Output directory file output already exists
  5. 【Elasticsearch】es keywods 类型数据 写入后查询不到
  6. 【elasticsearch】elasticsearch 生命周期 resourceAlreadyExistsException
  7. hive The specified datastore driver (“com.mysql.jdbc.Driver“) was not found
  8. spark学习-33-Spark安全机制SecurityManager
  9. 开源数据同步神器——canal
  10. RabbitMQ, ZeroMQ, Kafka 是一个层级的东西吗, 相互之间有哪些优缺点