hk2000c技术专栏

技术源于哲学,哲学来源于生活 关心生活,关注健康,关心他人

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  111 随笔 :: 1 文章 :: 28 评论 :: 0 Trackbacks
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 阅读(612) 评论(0)  编辑  收藏 所属分类: CMS项目开发

只有注册用户登录后才能发表评论。


网站导航: