<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 解決

    解決  回復  更多評論   


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


    網站導航:
     

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产日韩在线成人蜜芽 | 日韩精品视频免费在线观看| 国产成人综合亚洲| 曰韩亚洲av人人夜夜澡人人爽 | 香蕉蕉亚亚洲aav综合| 在线观看免费成人| 99久久久国产精品免费牛牛四川| 亚洲色大18成人网站WWW在线播放| 亚洲av最新在线网址| 亚洲精品视频在线看| 香蕉视频在线观看免费国产婷婷 | 久久精品亚洲一区二区三区浴池| 免费人成网站在线播放| 一二三四视频在线观看中文版免费| 在线人成免费视频69国产| 西西人体大胆免费视频| 黄色一级毛片免费看| 亚洲人成人网站18禁| avtt天堂网手机版亚洲| 亚洲人成网站在线播放影院在线| 亚洲欧洲美洲无码精品VA| 亚洲中文字幕不卡无码| 国产成人亚洲精品青草天美| 亚洲中文字幕久久精品无码APP| 四虎永久免费地址在线观看| 中文字幕av无码不卡免费| 最近免费字幕中文大全| 暖暖免费中文在线日本| 高潮毛片无遮挡高清免费视频 | 岛国精品一区免费视频在线观看| 亚洲精华液一二三产区| 亚洲hairy多毛pics大全| 亚洲av无码专区国产不乱码| 亚洲男人天堂2022| 亚洲欧美日韩自偷自拍| 精品亚洲国产成人av| 国产免费MV大全视频网站| jizz免费在线观看| 午夜精品一区二区三区免费视频 | 久久精品国产大片免费观看| 久久精品无码精品免费专区|