문제

There's a way to programatically add a beanPostProcessor to a ClassPathXmlApplicationContext ?

I can do it declaratively in the xml, but apparently there's no way to add programatically.

My processor must do something like if my bean is a MyInterfaceAware, then do setMyInterface(...).

I need to do it in code cause MyInterface implementation is instantiated in code, it's not available when starting the XML.

I'm using Spring 3.1.2.RELEASE.

Thanks,

... this is what I'm doing ...

public void setSpringBeanFactory(BeanFactory beanFactory) {
    this.beanFactory = (ApplicationContext) beanFactory;
    ((ClassPathXmlApplicationContext) beanFactory).getBeanFactory().addBeanPostProcessor(new BeanPostProcessor() {

        public Object postProcessBeforeInitialization(Object bean,
                String beanName) throws BeansException {
            return bean;
        }

        public Object postProcessAfterInitialization(Object bean,
                String beanName) throws BeansException {
            if (bean instanceof RegistryAware)
                ((RegistryAware) bean).setRegistry(ApplicationContextRegistryImpl.this);
            return bean;
        }
    });
    ((ClassPathXmlApplicationContext)beanFactory).refresh();
}
도움이 되었습니까?

해결책

Try with context.getBeanFactory().addBeanPostProcessor(beanPostProcessor)

Edit

You could use a BeanFactoryPostProcessor also:

public class RegistryBeanPostprocessorConfigurer implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        beanFactory.addBeanPostProcessor(getRegistryBeanPostProcessor());
    }
}

context.addBeanFactoryPostProcessor(new RegistryBeanPostProcessorConfigurer());
context.refresh();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top