Получить постпроцессоры bean на родительской фабрике bean для обработки bean на дочерних фабриках - PullRequest
5 голосов
/ 12 декабря 2010

У меня есть фабрика родительских бинов, и я бы хотел, чтобы в ней был BeanPostProcessor для пост-обработки бинов на дочерних фабриках.AFAIK, это не поддерживается весной.Каковы мои альтернативы?(кроме, конечно, для объявления постпроцессора в XML каждой дочерней фабрики)

1 Ответ

0 голосов
/ 24 марта 2015

Одним из "решений" является добавление постпроцессора компонента в дочерний контекст, который выполняет родительские постпроцессоры.Это техника, которую мы в конечном итоге использовали.Это потенциально опасная и не самая лучшая весенняя практика ИМО.

/**
 * 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;
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...