开发者

Get bean post processors in parent bean factory to process beans in child factories

开发者 https://www.devze.com 2023-01-29 22:32 出处:网络
I have a parent bean factory and I\'d like a BeanPostProcessor in it to post process beans in child factories. AFAIK, this is开发者_JAVA百科n\'t supported in Spring. What are my alternatives? (except

I have a parent bean factory and I'd like a BeanPostProcessor in it to post process beans in child factories. AFAIK, this is开发者_JAVA百科n't supported in Spring. What are my alternatives? (except of course to declare the post processor in the XML of each child factory)


One "solution" is to add a bean post processor to the child context that executes the parent post processors. This is the technique we ended up using. It is potentially dangerous and not the best Spring practice IMO.

/**
 * A {@linkplain BeanPostProcessor} that references the BeanPostProcessors in the parent context and applies them
 * to context this post processor is a part of. Any BeanPostProcessors from the parent that are {@link BeanFactoryAware} will
 * have the {@linkplain BeanFactory} from the child context set on them during the post processing. This is necessary to let such post processors
 * have access to the entire context.
 */
public class ParentContextBeanPostProcessor implements BeanPostProcessor {

  private final Collection<BeanPostProcessor> parentProcessors;
  private final BeanFactory beanFactory;
  private final BeanFactory parentBeanFactory;

  /**
   * @param parent the parent context
   * @param beanFactory the beanFactory associated with this post processor's context
   */
  public ParentContextBeanPostProcessor(ConfigurableApplicationContext parent, BeanFactory beanFactory) {
    this.parentProcessors = parent.getBeansOfType(BeanPostProcessor.class).values();
    this.beanFactory = beanFactory;
    this.parentBeanFactory = parent.getBeanFactory();
  }

  /** {@inheritDoc} */
  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    for (BeanPostProcessor processor : parentProcessors) {
      if (processor instanceof BeanFactoryAware) {
        ((BeanFactoryAware) processor).setBeanFactory(beanFactory);
      }
      try {
        bean = processor.postProcessBeforeInitialization(bean, beanName);
      } finally {
        if (processor instanceof BeanFactoryAware) {
          ((BeanFactoryAware) processor).setBeanFactory(parentBeanFactory);
        }
      }
    }
    return bean;
  }

  /** {@inheritDoc} */
  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    for (BeanPostProcessor processor : parentProcessors) {
      if (processor instanceof BeanFactoryAware) {
        ((BeanFactoryAware) processor).setBeanFactory(beanFactory);
      }
      try {
        bean = processor.postProcessAfterInitialization(bean, beanName);
      } finally {
        if (processor instanceof BeanFactoryAware) {
          ((BeanFactoryAware) processor).setBeanFactory(parentBeanFactory);
        }
      }
    }
    return bean;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号