Frage

We need to configure a bean as an infrastructure bean BeanDefinition.ROLE_INFRASTRUCTURE so that it gets considered by the InfrastructureAdvisorAutoProxyCreator.

However we cannot find a way to configure the role of the bean via xml.

Currently we had the bean implement BeanFactoryAware and then set the role inside setBeanFactory, something like this:

    ConfigurableListableBeanFactory configurableListableBeanFactory = (ConfigurableListableBeanFactory) beanFactory;
    AbstractBeanDefinition beanDefinition = (AbstractBeanDefinition) configurableListableBeanFactory.getBeanDefinition("hibernateSessionAnnotationAdvisor");
    beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

This is obviously not very nice and we would prefer to do this via xml.

Is there maybe a way to use the @Role annotation even though our beans are configured via xml?

War es hilfreich?

Lösung

You could use a BeanFactoryPostProcessor and configure the bean names (or types) that you want to change.

For example:

public class RoleBeanDefinitionConfigurer implements BeanDefinitionRegistryPostProcessor {

    private String[] beanNames;

    @Override
    public void postProcessBeanFactory(
            ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }

    @Override
    public void postProcessBeanDefinitionRegistry(
            BeanDefinitionRegistry registry) throws BeansException {

        if (beanNames == null)
            return;

        for (String name : beanNames) {
            if (registry.containsBeanDefinition(name))
                ((AbstractBeanDefinition) registry.getBeanDefinition(name)).setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        }
    }

    public String[] getBeanNames() {
        return beanNames;
    }

    public void setBeanNames(String[] beanNames) {
        this.beanNames = beanNames;
    }

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top