<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

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

    上面兩個工具方式的區別是,前者在獲取失敗時拋出異常,后者返回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
    說明:類似上面方法,調用getWebApplicationContext()獲取WebApplicationContext

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


    在web應用中一般用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加載
    在網上也找了些其他說法:實現ApplicationContextAware,,, 接口,或者servletcontextAware接口,還要寫配置文件。有的竟然要把配置文件里的listener,換成自己的類,這樣純粹多此一舉。不過有的應用不是替換,是在補一個listener,
    我在一版的jpetstore(具體那一版不知道)里發現了這個:
    [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實現接口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的構造里SpringInit獲取applicationcontext,代碼:
      public OrderBean() {
        
    this(
                (AccountService) SpringInit.getApplicationContext().getBean(
    "accountService"),
                (OrderService) SpringInit.getApplicationContext().getBean(
    "orderService") );
      }

    恩,這種在action,servlet之外的bean里獲取applicationcontext的方法值得參考,應該有用
    posted on 2009-10-22 18:02 xzc 閱讀(2378) 評論(0)  編輯  收藏 所屬分類: Spring
    主站蜘蛛池模板: 亚洲人成伊人成综合网久久久| 免费看男女下面日出水来| 国产男女猛烈无遮挡免费网站| 少妇中文字幕乱码亚洲影视| 国产午夜免费高清久久影院| 国产亚洲人成A在线V网站| 无码免费又爽又高潮喷水的视频| 免费一级毛片不卡不收费| 水蜜桃视频在线观看免费| 亚洲国产精品日韩专区AV| 久久免费香蕉视频| 亚洲精品乱码久久久久久| 日本免费中文视频| 亚洲日产2021三区在线 | 老汉精品免费AV在线播放| 亚洲av中文无码乱人伦在线r▽ | 亚洲午夜无码久久久久| 久久99热精品免费观看动漫| 亚洲春黄在线观看| 女性无套免费网站在线看| 野花视频在线官网免费1| 久久青青草原亚洲av无码| 精品视频在线免费观看| 亚洲精品视频在线免费| 免费无码又爽又刺激高潮的视频 | 亚洲欧美乱色情图片| 亚洲第一页日韩专区| 国产三级在线免费| 亚洲AV一二三区成人影片| 四虎在线播放免费永久视频 | 亚洲an日韩专区在线| gogo全球高清大胆亚洲| 热久久这里是精品6免费观看| 亚洲精品中文字幕无码AV| 日韩毛片免费在线观看| 三级网站在线免费观看| 亚洲熟女乱色一区二区三区| 亚洲精品国产日韩无码AV永久免费网| 久久精品免费观看| 亚洲国产成人无码AV在线影院| 亚洲色欲久久久综合网|