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

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

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

    java學習

    java學習

     

    ApplicationContextAware接口的作用

    加載Spring配置文件時,如果Spring配置文件中所定義的Bean類實現了ApplicationContextAware 接口,那么在加載Spring配置文件時,會自動調用ApplicationContextAware 接口中的

    public void setApplicationContext(ApplicationContext context) throws BeansException

    方法,獲得ApplicationContext對象。

    前提必須在Spring配置文件中指定該類

    public class ApplicationContextRegister implements ApplicationContextAware {

    private Log log = LogFactory.getLog(getClass());

    public void setApplicationContext(ApplicationContext applicationContext)
       throws BeansException {
      ContextUtils.setApplicationContext(applicationContext);
      log.debug("ApplicationContext registed");
    }

    }

    public class ContextUtils {

    private static ApplicationContext applicationContext;

    private static Log log = LogFactory.getLog(ContextUtils.class);

    public static void setApplicationContext(ApplicationContext applicationContext) {
      synchronized (ContextUtils.class) {
       log.debug("setApplicationContext, notifyAll");
       ContextUtils.applicationContext = applicationContext;
       ContextUtils.class.notifyAll();
      }
    }

    public static ApplicationContext getApplicationContext() {
      synchronized (ContextUtils.class) {
       while (applicationContext == null) {
        try {
         log.debug("getApplicationContext, wait...");
         ContextUtils.class.wait(60000);
         if (applicationContext == null) {
          log.warn("Have been waiting for ApplicationContext to be set for 1 minute", new Exception());
         }
        } catch (InterruptedException ex) {
         log.debug("getApplicationContext, wait interrupted");
        }
       }
       return applicationContext;
      }
    }

    public static Object getBean(String name) {
      return getApplicationContext().getBean(name);
    }

    }

    配置文件:<bean class="com.sinotrans.framework.core.support.ApplicationContextRegister" />

    正常情況:

    1. public class AppManager extends ContextLoaderListener implements  ServletContextListener {  
    2.  
    3.     private ServletContext context;  
    4.     private WebApplicationContext webApplicationContext;  
    5.  
    6.     public void contextInitialized(ServletContextEvent sce) {  
    7.         this.context = sce.getServletContext();  
    8.         this.webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);  
    9.         this.context.setAttribute("WEBAPPLICATIONCONTEXT", webApplicationContext);       } 
    1. HttpSession session = request.getSession();  
    2. WebApplicationContext webApplicationContext = (WebApplicationContext)session.getServletContext().getAttribute("WEBAPPLICATIONCONTEXT");  
    3. UnsubscribeEmailFacade unsubscribeEmailFacade = (UnsubscribeEmailFacade)webApplicationContext.getBean("unsubscribeEmailFacade"); 

    posted on 2013-08-23 09:29 楊軍威 閱讀(37194) 評論(1)  編輯  收藏

    評論

    # re: ApplicationContextAware接口的作用 2015-08-05 13:45 解決

    解決  回復  更多評論   


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲色精品三区二区一区| 亚洲综合丁香婷婷六月香| 国产A∨免费精品视频| 免费吃奶摸下激烈视频| 美女露100%胸无遮挡免费观看| 国产在线观看免费不卡| 黄网站色视频免费观看45分钟| 免费a级毛片在线观看| aa在线免费观看| 亚洲成a人片77777老司机| 99免费视频观看| 亚洲首页国产精品丝袜| 免费看的黄色大片| 四虎精品成人免费视频| 亚洲欧洲日产国码无码久久99| 欧洲人免费视频网站在线| 亚洲国产精品综合久久2007| 最新猫咪www免费人成| 美女视频黄频a免费| 国产亚洲一区二区三区在线| 67pao强力打造国产免费| 亚洲 日韩经典 中文字幕| 亚洲av高清在线观看一区二区| 国产一级高青免费| 亚洲精品欧洲精品| 国产精品嫩草影院免费| 国产精品小视频免费无限app| 亚洲视频在线观看免费| 成年女人免费碰碰视频| 精品无码国产污污污免费网站国产 | 四虎在线成人免费网站| 亚洲精品无码人妻无码| 国产亚洲精品AA片在线观看不加载| 国产精品区免费视频| 亚洲一日韩欧美中文字幕在线| yy6080久久亚洲精品| 99久久综合精品免费| 精品久久久久亚洲| 亚洲国语精品自产拍在线观看| 精品国产免费一区二区| 免费看搞黄视频网站|