<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-314  評論-209  文章-0  trackbacks-0

    轉(zhuǎn)自:http://www.tkk7.com/Todd/archive/2009/09/15/295112.html
    方法一:在初始化時(shí)保存ApplicationContext對象
    代碼:
    ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
    ac.getBean("beanId");
    說明:這種方式適用于采用Spring框架的獨(dú)立應(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對象,然后在通過它獲取需要的類實(shí)例。

    上面兩個(gè)工具方式的區(qū)別是,前者在獲取失敗時(shí)拋出異常,后者返回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初始化時(shí),會(huì)通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。

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

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


    在web應(yīng)用中一般用ContextLoaderListener加載webapplication,如果需要在action之外或者control類之外獲取webapplication思路之一是,單獨(dú)寫個(gè)類放在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)上也找了些其他說法:實(shí)現(xiàn)ApplicationContextAware,,, 接口,或者servletcontextAware接口,還要寫配置文件。有的竟然要把配置文件里的listener,換成自己的類,這樣純粹多此一舉。不過有的應(yīng)用不是替換,是在補(bǔ)一個(gè)listener,
    我在一版的jpetstore(具體那一版不知道)里發(fā)現(xiàn)了這個(gè):
    [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實(shí)現(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;
        }

        
    }

    在其中的一個(gè)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-10-22 18:02 xzc 閱讀(2370) 評論(0)  編輯  收藏 所屬分類: Spring
    主站蜘蛛池模板: 免费视频成人手机在线观看网址| 在人线av无码免费高潮喷水| 久久国产亚洲观看| 国产h视频在线观看免费| 亚洲精品美女久久7777777 | 国内一级一级毛片a免费| 性生大片视频免费观看一级 | 亚洲av永久无码精品漫画| 久久九九兔免费精品6| 国产亚洲综合视频| 亚洲激情中文字幕| 四虎影视永久免费观看地址| 日本免费A级毛一片| 亚洲国产视频久久| 日本亚洲视频在线| 国产美女无遮挡免费视频网站| 四虎国产精品免费永久在线| 亚洲五月综合网色九月色| 亚洲人成人一区二区三区| 超pen个人视频国产免费观看| 午夜理伦剧场免费| 四虎国产精品成人免费久久| 亚洲人6666成人观看| 国产亚洲一区二区手机在线观看| 无码人妻精品一二三区免费| 免费91麻豆精品国产自产在线观看| 久久精品国产亚洲av天美18 | 免费无码午夜福利片69| 亚洲成aⅴ人在线观看| 黑人精品videos亚洲人| 国产一级做a爱免费视频| 国产成人yy免费视频| a毛片在线看片免费| 男女猛烈激情xx00免费视频| 精品久久久久久亚洲精品| 久久久久亚洲AV无码麻豆| 亚洲一区精品无码| 亚洲乱码中文字幕手机在线 | 免费观看无遮挡www的视频 | 1000部啪啪未满十八勿入免费| a免费毛片在线播放|