天天看點

Spring中ApplicationContext加載機制

加載器目前有兩種選擇:contextloaderlistener和contextloaderservlet。 

   這兩者在功能上完全等同,隻是一個是基于servlet2.3版本中新引入的listener接口實作,而另一個基于servlet接口實作。開發中可根據目标web容器的實際情況進行選擇。 

配置非常簡單,在web.xml中增加: 

<listener> 

       <listener-class> 

          org.springframework.web.context.contextloaderlistener 

       </listener-class> 

</listener> 

或: 

<servlet> 

         <servlet-name>context</servlet-name> 

         <servlet-class> 

           org.springframework.web.context.contextloaderservlet 

         </servlet-class> 

         <load-on-startup>1</load-on-startup> 

</servlet> 

通過以上配置,web容器會自動加載/web-inf/applicationcontext.xml初始化 

applicationcontext執行個體,如果需要指定配置檔案位置,可通過context-param加以指定: 

<context-param><param-name>contextconfiglocation</param-name>        <param-value>classpath:applicationcontext-bean.xml,classpath:spring-jamon.xml</param-value> </context-param> 

配置完成之後,即可通過 

webapplicationcontextutils.getwebapplicationcontext方法在web應用中擷取applicationcontext引用。 

如:applicationcontext ctx=webapplicationcontextutils.getwebapplicationcontext(); 

         loginaction action=(loginaction)ctx.getbean("action"); 

------------------------------------------------------------------------------------------- 

spring為applicationcontext提供有多種實作(舉例) 

spring為applicationcontext提供的3種實作分别為:classpathxmlapplicationcontext,filesystemxmlapplicationcontext和xmlwebapplicationcontext,其中xmlwebapplicationcontext是專為web工程定制的。使用舉例如下: 

   1. filesystemxmlapplicationcontext 

      eg1.applicationcontext ctx = new filesystemxmlapplicationcontext("bean.xml"); //加載單個配置檔案 

      eg2.string[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"}; 

          applicationcontext ctx = new filesystemxmlapplicationcontext(locations ); //加載多 

個配置檔案 

eg3.applicationcontext ctx =new filesystemxmlapplicationcontext("d:/project/bean.xml");//根據具體路徑加載檔案 

注: 

(1)這種方式适用于采用spring架構的獨立應用程式,需要程式通過配置檔案手工初始化spring的情況。 

(2)擷取失敗時抛出異常。 

  2. classpathxmlapplicationcontext 

      eg1.applicationcontext ctx = new classpathxmlapplicationcontext("bean.xml"); 

           applicationcontext ctx = new classpathxmlapplication(locations); 

(1).其中filesystemxmlapplicationcontext和classpathxmlapplicationcontext與beanfactory的xml檔案定位方式一      樣是基于路徑的。 

3. xmlwebapplicationcontext 

    eg1.servletcontext servletcontext = request.getsession().getservletcontext();    

            applicationcontext ctx = webapplicationcontextutils.getwebapplicationcontext(servletcontext); 

注 : 

(1)一般是 applicationcontext ctx = 

webapplicationcontextutils.getwebapplicationcontext(this.getservletcontext()); 

(2)擷取失敗時傳回null。 

(3)這種方式适合于采用spring架構的b/s系統,通過servletcontext對象擷取applicationcontext對象,然後在通過它擷取需要的類執行個體。 

4.繼承自抽象類applicationobjectsupport 

說明:抽象類applicationobjectsupport提供getapplicationcontext()方法,可以友善的擷取到applicationcontext。 

spring初始化時,會通過該抽象類的setapplicationcontext(applicationcontext context)方法将applicationcontext 對象注入。 

5.繼承自抽象類webapplicationobjectsupport 

說明:類似上面方法,調用getwebapplicationcontext()擷取webapplicationcontext 

6.實作接口applicationcontextaware 

說明:實作該接口的setapplicationcontext(applicationcontext context)方法,并儲存applicationcontext 對象。 

spring初始化時,會通過該方法将applicationcontext對象注入。 

實作方法: 

public void setapplicationcontext(applicationcontext arg0) throws beansexception { 

        applicationcontext = arg0;    

    } 

擷取bean: 

itaskservice bean = (itaskservice)applicationcontext.getbean(taskservicename); 

參考: 

1 http://hi.baidu.com/fyears/blog/item/e2f689afd05216ce7dd92a37.html 

2. http://www.blogjava.net/todd/archive/2010/04/22/295112.html 

特别說明:尊重作者的勞動成果,轉載請注明出處哦~~~

http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp33