2010-12-12 11 views
5

Ich habe eine Eltern-Bean-Factory und möchte einen BeanPostProcessor darin, Process-Beans in untergeordneten Fabriken zu veröffentlichen. AFAIK, dies wird im Frühjahr nicht unterstützt. Was sind meine Alternativen? (außer natürlich, den Postprozessor in der XML jeder untergeordneten Fabrik zu deklarieren)Bean-Post-Prozessoren in der Mutter-Bean-Factory abrufen, um Beans in untergeordneten Fabriken zu verarbeiten

+0

Mir sind keine anderen Alternativen bekannt, abgesehen davon, dass ich Eltern-Kind-Kontexte sparsamer verwende. – skaffman

Antwort

0

Eine "Lösung" besteht darin, einen Bean-Post-Prozessor zu dem Kindkontext hinzuzufügen, der die übergeordneten Postprozessoren ausführt. Dies ist die Technik, die wir verwendet haben. Es ist potentiell gefährlich und nicht die beste 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; 
    } 
} 
Verwandte Themen