java學(xué)習(xí)

          java學(xué)習(xí)

           

          ApplicationContextAware接口的作用

          加載Spring配置文件時,如果Spring配置文件中所定義的Bean類實(shí)現(xiàn)了ApplicationContextAware 接口,那么在加載Spring配置文件時,會自動調(diào)用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 解決

          解決  回復(fù)  更多評論   


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 玉溪市| 绿春县| 濮阳市| 曲靖市| 炉霍县| 吴忠市| 泾阳县| 龙海市| 旌德县| 耒阳市| 龙江县| 石台县| 临沭县| 老河口市| 柯坪县| 西吉县| 黄龙县| 简阳市| 德格县| 宜章县| 建瓯市| 安吉县| 房山区| 彭山县| 大石桥市| 榆社县| 和静县| 延寿县| 武功县| 海盐县| 资阳市| 德惠市| 浪卡子县| 化州市| 新巴尔虎右旗| 正宁县| 三明市| 柳州市| 玉田县| 金门县| 大英县|