java JavaConfig 中的 Spring Bean 别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27107133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:14:26  来源:igfitidea点击:

Spring Bean Alias in JavaConfig

javaspringdependency-injectionspring-bean

提问by dtrunk

I have a @Serviceannotated class which provides core functionality which I can use in all my projects:

我有一个带@Service注释的类,它提供了我可以在所有项目中使用的核心功能:

@Service
public class MyService {}

and another one which extends it to implement project specific stuff:

另一个扩展它以实现项目特定的东西:

@Service
public class ExtendedMyService extends MyService {}

Now I would like to configure a bean alias to be able to use @Qualifier("MyServiceAlias")when autowiring it using a property:

现在我想配置一个 bean 别名,以便@Qualifier("MyServiceAlias")在使用属性自动装配它时能够使用:

# MyService qualifier (default: myService)
myService.qualifier=extendedMyService

In XML it would look like:

在 XML 中,它看起来像:

<alias name="${myService.qualifier}" alias="MyServiceAlias" />

It is also discussed here, but I need to do it w/o XML, JavaConfig only. Is it possible and how to realize?

这里也讨论了它,但我只需要在没有 XML、JavaConfig 的情况下进行。是否可能以及如何实现?

回答by Evgeni Dimitrov

There is an open Jira for this: https://jira.spring.io/browse/SPR-6736

为此有一个开放的 Jira:https: //jira.spring.io/browse/SPR-6736

The workaround is to use @Beanin @Configurationclass:

解决方法是@Bean@Configuration课堂上使用:

@Configuration
public class AppConfig {

  @Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })
  public MyService myService() {}

}

回答by herau

If you want to use the placeholder, another workaround is to use @Bean in a @Configuration class using @Value and the Spring applicationContext.

如果要使用占位符,另一种解决方法是使用 @Value 和 Spring applicationContext 在 @Configuration 类中使用 @Bean。

@Configuration
public class AppConfig {

    @Autowired
    private ApplicationContext context;

    @Bean
    public MyService myService(@Value("${myService.qualifier}") String qualifier) {
        return (MyService) context.getBean(qualifier);
    }
}

NB : special consideration must be taken for the placeholder bean which must be loaded at the beginning (cf javadoc)

注意:必须特别考虑必须在开头加载的占位符 bean (cf javadoc)

回答by jankovd

With small amount of configuration and one ImportBeanDefinitionRegistraryou can configure bean aliases via Java configuration. You can check bean-aliaslibrary project for reference - developed for the needs of my projects. Feel free to modify and/or copy the source into your own project in case the spring version used in it does not work with your setup.

ImportBeanDefinitionRegistrar只需少量配置,您就可以通过 Java 配置配置 bean 别名。您可以查看bean-alias库项目以供参考 - 为我的项目需要而开发。随意修改和/或将源代码复制到您自己的项目中,以防其中使用的 spring 版本不适用于您的设置。

Once you have the library on your path, you declare an alias through the annotation:

一旦你的路径上有了库,你就可以通过注释声明一个别名:

@Configuration
@BeanAlias(name = "fromName", alias = "toName")
public class ExampleConfiguration {
}

That's it.

而已。

How it works is that with the annotation we import a ImportBeanDefinitionRegistrarimplementation

它的工作原理是使用注释导入一个ImportBeanDefinitionRegistrar实现

@Import(BeanAliasBeanRegistrar.class)
public @interface BeanAlias {
}

which registers the alias in the BeanDefinitionRegistry

它将别名注册在 BeanDefinitionRegistry

class BeanAliasBeanRegistrar implements ImportBeanDefinitionRegistrar, PriorityOrdered {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
      ...
      registerAlias(registry, metadata.getAnnotationAttributes(BeanAlias.class.getName()));
    }

    private void registerAlias(BeanDefinitionRegistry registry, Map<String, Object> attributes) {
      ...
      registry.registerAlias(name, alias);
    }
}