Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Bean Alias in JavaConfig

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 {}

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

In XML it would look like:

<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?

like image 471
dtrunk Avatar asked Nov 24 '14 14:11

dtrunk


1 Answers

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

The workaround is to use @Bean in @Configuration class:

@Configuration
public class AppConfig {

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

}
like image 129
Evgeni Dimitrov Avatar answered Sep 25 '22 15:09

Evgeni Dimitrov