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 楊軍威 閱讀(37200) 評論(1)  編輯  收藏

          評論

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

          解決  回復  更多評論   


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 榆树市| 黔南| 泾川县| 方正县| 射阳县| 巴中市| 武义县| 深圳市| 河间市| 诏安县| 淄博市| 清流县| 新宁县| 彭州市| 枝江市| 雷州市| 内乡县| 宜春市| 慈溪市| 林甸县| 岐山县| 建水县| 临沭县| 永和县| 合川市| 大同县| 怀集县| 孝义市| 清丰县| 仙居县| 保靖县| 西乌| 南陵县| 石棉县| 台中市| 莆田市| 恩平市| 溆浦县| 惠州市| 孟州市| 翁源县|