Aliasing a bean outside the bean definition using Java config in Spring Boot

萝らか妹 提交于 2020-05-29 08:17:16

问题


How to alias a bean outside the bean definition using Java config in Spring Boot?


回答1:


I have this as well, and solved it like this:

@Component
public class AliasConfiguration implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        beanFactory.registerAlias("originalBeanName", "newAlias");
        beanFactory.registerAlias("originalBeanName", "newAlias2");
        beanFactory.registerAlias("otherOriginalBeanName", "newAlias3");
    }
}



回答2:


You want to alias a bean which is already defined somewhere else, this feature is not supported in spring yet.

Along with that aliasing a bean is not allowed in @Component, @Service and @Repository.

Either you can alias a bean while defining in XML configuration or while using @Bean(name = {"alias1", "alias2"}). But as you mentioned in you case bean is already defined in another JAR, it's not possible to alias it.

A similar(not exactly similar) issue is open to spring-framework.



来源:https://stackoverflow.com/questions/54938127/aliasing-a-bean-outside-the-bean-definition-using-java-config-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!