hk2000c技術專欄

          技術源于哲學,哲學來源于生活 關心生活,關注健康,關心他人

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            111 隨筆 :: 1 文章 :: 28 評論 :: 0 Trackbacks
          <2007年10月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          Java Blog

          liferay

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          BaseCommandController extends AbstractController extends WebContentGenerator WebContentGenerator extends WebApplicationObjectSupport extends ApplicationObjectSupport implements ApplicationContextAware 

          /**
            * Subclasses can override this for custom initialization behavior.
            * Gets called by <code>setApplicationContext</code> after setting the context instance.
            * <p>Note: Does </i>not</i> get called on reinitialization of the context
            * but rather just on first initialization of this object's context reference.
            * @throws ApplicationContextException in case of initialization errors
            * @throws BeansException if thrown by ApplicationContext methods
            * @see #setApplicationContext
            */
           protected void initApplicationContext() throws BeansException {
           }

          愿意為子類可以把初始化bean 動作放入此方法,可以自定義一些動作。

          我們再來看看調用
          public final void setApplicationContext(ApplicationContext context) throws BeansException {
            if (context == null && !isContextRequired()) {
             // Reset internal context state.
             this.applicationContext = null;
             this.messageSourceAccessor = null;
            }
            else if (this.applicationContext == null) {
             // Initialize with passed-in context.
             if (!requiredContextClass().isInstance(context)) {
              throw new ApplicationContextException(
                "Invalid application context: needs to be of type [" + requiredContextClass().getName() + "]");
             }
             this.applicationContext = context;
             this.messageSourceAccessor = new MessageSourceAccessor(context);
             initApplicationContext();
            }
            else {
             // Ignore reinitialization if same context passed in.
             if (this.applicationContext != context) {
              throw new ApplicationContextException(
                "Cannot reinitialize with different application context: current one is [" +
                this.applicationContext + "], passed-in one is [" + context + "]");
             }
            }
           }

          可以看到由 ApplicationObjectSupport  的 setApplicationContext 方法調用
          而此方法為 ApplicationContextAware 的唯一接口方法,

          public interface ApplicationContextAware {
           
           /**
            * Set the ApplicationContext that this object runs in.
            * Normally this call will be used to initialize the object.
            * <p>Invoked after population of normal bean properties but before an init callback such
            * as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
            * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
            * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
            * {@link MessageSourceAware}, if applicable.
            * @param applicationContext the ApplicationContext object to be used by this object
            * @throws ApplicationContextException in case of context initialization errors
            * @throws BeansException if thrown by application context methods
            * @see org.springframework.beans.factory.BeanInitializationException
            */
           void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

          }

          此方法被 ApplicationContextAwareProcessor 的 postProcessBeforeInitialization  調用
          ApplicationContextAwareProcessor implements BeanPostProcessor 
           public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof ResourceLoaderAware) {
             ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
            }
            if (bean instanceof ApplicationEventPublisherAware) {
             ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
            }
            if (bean instanceof MessageSourceAware) {
             ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
            }
            if (bean instanceof ApplicationContextAware) {
             ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
            }
            return bean;
           }



          posted on 2007-10-26 01:12 hk2000c 閱讀(624) 評論(0)  編輯  收藏 所屬分類: CMS項目開發
          主站蜘蛛池模板: 昌图县| 娱乐| 金寨县| 琼结县| 四川省| 清丰县| 北宁市| 陆河县| 河东区| 湘潭县| 顺平县| 桐乡市| 南乐县| 马山县| 蒙自县| 吴旗县| 临夏县| 秦安县| 高雄市| 高陵县| 汕头市| 深圳市| 青阳县| 玛沁县| 新密市| 通辽市| 星座| 桂阳县| 逊克县| 宁明县| 定州市| 隆回县| 芮城县| 卫辉市| 遵义市| 石渠县| 化隆| 铁力市| 夏邑县| 东平县| 光山县|