JavaConfig中的Spring Bean别名 [英] Spring Bean Alias in JavaConfig
问题描述
我有一个 @Service
带注释的类,它提供了我可以在所有项目中使用的核心功能:
I have a @Service
annotated class which provides core functionality which I can use in all my projects:
@Service
public class MyService {}
另一个扩展它以实现项目特定的东西:
and another one which extends it to implement project specific stuff:
@Service
public class ExtendedMyService extends MyService {}
现在我想配置一个bean别名,以便能够使用 @Qualifier (MyServiceAlias)
使用属性自动装配时:
Now I would like to configure a bean alias to be able to use @Qualifier("MyServiceAlias")
when autowiring it using a property:
# MyService qualifier (default: myService)
myService.qualifier=extendedMyService
在XML中它看起来像:
In XML it would look like:
<alias name="${myService.qualifier}" alias="MyServiceAlias" />
还讨论了这里,但我只需要使用XML,JavaConfig。
是否有可能以及如何实现?
It is also discussed here, but I need to do it w/o XML, JavaConfig only. Is it possible and how to realize?
推荐答案
这是一个开放的Jira: https://jira.spring.io/browse/SPR-6736
There is an open Jira for this: https://jira.spring.io/browse/SPR-6736
解决方法是在 @Configuration
类中使用 @Bean
:
The workaround is to use @Bean
in @Configuration
class:
@Configuration
public class AppConfig {
@Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })
public MyService myService() {}
}
这篇关于JavaConfig中的Spring Bean别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!