作者:江南白衣    

    Spring再?gòu)?qiáng)大,也要面對(duì)降臨的問(wèn)題--因?yàn)镾pring不是Weblogic、Tomcat般的頂層容器,Servlet和EJB對(duì)象不由它創(chuàng)建,所以它必須要降臨到Weblogic、Tomcat所在的位面。
     初學(xué)者一般不用管那么多,照著Spring+hibernate+Struts之類(lèi)的Sample就做了,但慢慢的,也許就要開(kāi)始在jsp+javabean體系,土制框架,singleton類(lèi)等環(huán)境下使用Spring了。
     《Professional Java Development with the Spring Framework》第3章有"Managing the Containe"一節(jié)講這個(gè)問(wèn)題。一般可以分為直接召喚系與IoC fashion兩類(lèi)。

     1.直接召喚系--Singleton的Application Context
      最簡(jiǎn)單的,就像在UnitTest里那樣,直接構(gòu)造Application Context:

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

    
      在Web環(huán)境里,會(huì)使用ContextLoader構(gòu)造ApplicationContext后,壓進(jìn)Servlet Context。
      由ContextLoaderListener或ContextLoaderServlet,在Web應(yīng)用啟動(dòng)時(shí)完成。
      然后在Jsp/Servelet中,可以通過(guò)Servlet Context取得ApplicationContext:
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application);

     
      但像singleton類(lèi)或者EJB中,就沒(méi)有Servlet Context可用了。
      如果全部像UnitTest那樣直接構(gòu)造,速度就會(huì)很不堪。自然的,就想到把ApplicationContext做成單例。 
      Spring提供了ContextSingletonBeanFactoryLocator這樣的物體。
      先搞一個(gè)beanRefFactory.xml,里面寫(xiě)上所有的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去找它,但代碼有點(diǎn)長(zhǎng):
BeanFactoryReference bfr =  DefaultLocatorFactory.getInstance().useBeanFactory("default-context");
BeanFactory factory = bfr.getFactory();
MyService myService 
= factory.getBean("myService");
bfr.release();
// now use myService


    上面的代碼實(shí)在是太靈活,太麻煩了。
    還不如自己實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Singleton,擴(kuò)展ContextLoaderListener類(lèi),在Web系統(tǒng)啟動(dòng)時(shí)壓入Singleton。

   
新的ContextLoaderListener類(lèi)重載如下,ContextUtil中包含一個(gè)靜態(tài)的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的耳光了。因?yàn)樗恢倍挤磳?duì)代碼與框架深耦合的。
    所以,更好的方法是寫(xiě)一些glue code、base class來(lái)完成Spring的降臨,而不讓?xiě)?yīng)用代碼察覺(jué)Spring Application Context的存在。
    不過(guò),因?yàn)楦鱾€(gè)框架的結(jié)構(gòu)不同,Rod也沒(méi)辦法講出一個(gè)通用的整合方法,所以建議大家盡量學(xué)習(xí)已整合的各種框架,如Spring MVC、Struts的種種方式,寫(xiě)出自己的簡(jiǎn)單整合代碼來(lái)。

    只有不確定的調(diào)用某些Singleton類(lèi),不適合過(guò)早ioc的情況,可以使用直接召喚系。