Todd

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            65 隨筆 :: 0 文章 :: 24 評論 :: 0 Trackbacks

          方法一:在初始化時保存ApplicationContext對象
          代碼:
          ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
          ac.getBean("beanId");
          說明:這種方式適用于采用Spring框架的獨立應(yīng)用程序,需要程序通過配置文件手工初始化Spring的情況。

          方法二:通過Spring提供的工具類獲取ApplicationContext對象
          代碼:
          import org.springframework.web.context.support.WebApplicationContextUtils;
          ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
          ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
          ac1.getBean("beanId");
          ac2.getBean("beanId");
          說明:
          這種方式適合于采用Spring框架的B/S系統(tǒng),通過ServletContext對象獲取ApplicationContext對象,然后在通過它獲取需要的類實例。

          上面兩個工具方式的區(qū)別是,前者在獲取失敗時拋出異常,后者返回null。

          其中 servletContext sc 可以具體 換成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext(); 另外,由于spring是注入的對象放在ServletContext中的,所以可以直接在ServletContext取出 WebApplicationContext 對象: WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

          方法三:繼承自抽象類ApplicationObjectSupport
          說明:抽象類ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取到ApplicationContext。
          Spring初始化時,會通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。

          方法四:繼承自抽象類WebApplicationObjectSupport
          說明:類似上面方法,調(diào)用getWebApplicationContext()獲取WebApplicationContext

          方法五:實現(xiàn)接口ApplicationContextAware
          說明:實現(xiàn)該接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 對象。
          Spring初始化時,會通過該方法將ApplicationContext對象注入。


          在web應(yīng)用中一般用ContextLoaderListener加載webapplication,如果需要在action之外或者control類之外獲取webapplication思路之一是,單獨寫個類放在static變量中,
          類似于:
          public class AppContext {

            
          private static AppContext instance;

            
          private AbstractApplicationContext appContext;

            
          public synchronized static AppContext getInstance() {
              
          if (instance == null) {
                instance 
          = new AppContext();
              }
              
          return instance;
            }

            
          private AppContext() {
              
          this.appContext = new ClassPathXmlApplicationContext(
                  
          "/applicationContext.xml");
            }

            
          public AbstractApplicationContext getAppContext() {
              
          return appContext;
            }
          }

          不過這樣,還是加載了2次applicationcontext,servlet一次,路徑加載一次;覺得不如直接用路徑加載,舍掉servlet加載
          在網(wǎng)上也找了些其他說法:實現(xiàn)ApplicationContextAware,,, 接口,或者servletcontextAware接口,還要寫配置文件。有的竟然要把配置文件里的listener,換成自己的類,這樣純粹多此一舉。不過有的應(yīng)用不是替換,是在補一個listener,
          我在一版的jpetstore(具體那一版不知道)里發(fā)現(xiàn)了這個:
          [web.xml]里
               
              <listener>
                  
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
              
          </listener>
              
              
          <listener>
                  
          <listener-class>com.ibatis.jpetstore.util.SpringInit</listener-class>
              
          </listener>
          其中SpringInit實現(xiàn)接口ServletContextListener

          package com.ibatis.jpetstore.util;

          import javax.servlet.ServletContextEvent;
          import javax.servlet.ServletContextListener;
          import org.springframework.context.ApplicationContext;
          import org.springframework.web.context.WebApplicationContext;
          import org.springframework.web.context.support.WebApplicationContextUtils;


          public class SpringInit implements ServletContextListener {
              

              
          private static WebApplicationContext springContext;
              
              
          public SpringInit() {
                  
          super();
              }
              
              
          public void contextInitialized(ServletContextEvent event) {
                  springContext 
          = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
              }
              

              
          public void contextDestroyed(ServletContextEvent event) {
              }
              
              
          public static ApplicationContext getApplicationContext() {
                  
          return springContext;
              }

              
          }

          在其中的一個bean的構(gòu)造里SpringInit獲取applicationcontext,代碼:
            public OrderBean() {
              
          this(
                      (AccountService) SpringInit.getApplicationContext().getBean(
          "accountService"),
                      (OrderService) SpringInit.getApplicationContext().getBean(
          "orderService") );
            }

          恩,這種在action,servlet之外的bean里獲取applicationcontext的方法值得參考,應(yīng)該有用
          posted on 2009-09-15 11:11 Todd 閱讀(82835) 評論(12)  編輯  收藏 所屬分類: spring

          評論

          # re: spring獲取webapplicationcontext,applicationcontext幾種方法詳解 2010-04-22 02:05 netnetnet
          后面那段值得參考。  回復(fù)  更多評論
            

          # re: spring獲取webapplicationcontext,applicationcontext幾種方法詳解 2010-06-09 16:27 qianqf
          public OrderBean() {
          this(
          (AccountService) SpringInit.getApplicationContext().getBean("accountService"),
          (OrderService) SpringInit.getApplicationContext().getBean("orderService") );
          }
          這個在哪里寫的???????????????????????????????????????????????????  回復(fù)  更多評論
            

          # re: spring獲取webapplicationcontext,applicationcontext幾種方法詳解 2010-06-21 13:50 Todd
          @qianqf
          這是一個service bean里的構(gòu)造函數(shù)  回復(fù)  更多評論
            

          # re: spring獲取webapplicationcontext,applicationcontext幾種方法詳解 2010-08-24 13:49 kj
          2134243  回復(fù)  更多評論
            

          # re: spring獲取webapplicationcontext,applicationcontext幾種方法詳解[未登錄] 2012-04-27 13:56 石頭
          石頭  回復(fù)  更多評論
            

          # re: spring獲取webapplicationcontext,applicationcontext幾種方法詳解[未登錄] 2012-04-27 13:56 石頭
          不錯值得學(xué)習(xí)  回復(fù)  更多評論
            

          # re: spring獲取webapplicationcontext,applicationcontext幾種方法詳解 2013-04-09 09:54 sf
          sf  回復(fù)  更多評論
            

          # re: spring獲取webapplicationcontext,applicationcontext幾種方法詳解 2014-06-03 17:19 asdf
          asdf  回復(fù)  更多評論
            

          # re: spring獲取webapplicationcontext,applicationcontext幾種方法詳解 2014-06-03 17:20 kj
          闊以  回復(fù)  更多評論
            

          # re: spring獲取webapplicationcontext,applicationcontext幾種方法詳解 2014-06-03 17:21 天貓
          @kj
          @kj
          @kj
          @kj
          simple  回復(fù)  更多評論
            

          # re: spring獲取webapplicationcontext,applicationcontext幾種方法詳解 2014-11-17 15:19 yo
          public class AppContext {

          private static AppContext instance;

          private AbstractApplicationContext appContext;

          public synchronized static AppContext getInstance() {
          if (instance == null) {
          instance = new AppContext();
          }
          return instance;
          }

          private AppContext() {
          this.appContext = new ClassPathXmlApplicationContext(
          "/applicationContext.xml");
          }

          public AbstractApplicationContext getAppContext() {
          return appContext;
          }
          }

          你好,這樣子不是相當于重新new了一個ApplicationContext出來么?
          原先用ContextLoaderListener加載webapplication時也new了一個
          那整個系統(tǒng)中不是就存在兩個spring容器了?
          這里很疑惑,能幫我解釋一下么?  回復(fù)  更多評論
            

          # re: spring獲取webapplicationcontext,applicationcontext幾種方法詳解 2014-11-28 18:02 Todd
          兩個Context,如果你需要在action或control以外的地方用到Context,就再生成一個Context,否則不需要多此一舉@yo
            回復(fù)  更多評論
            

          主站蜘蛛池模板: 清河县| 唐山市| 嘉鱼县| 凭祥市| 中卫市| 西安市| 潍坊市| 鄯善县| 金华市| 榆中县| 梨树县| 连南| 堆龙德庆县| 鹤壁市| 盖州市| 衡南县| 湟源县| 永年县| 贡山| 体育| 乐清市| 化德县| 嘉祥县| 泰顺县| 娱乐| 鹿泉市| 龙里县| 洛隆县| 宣汉县| 庄河市| 浮山县| 普格县| 霍州市| 旺苍县| 成安县| 临颍县| 临沧市| 阿拉尔市| 盱眙县| 于田县| 长丰县|