13

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?

Community
  • 1
  • 1
dtrunk
  • 4,685
  • 17
  • 65
  • 109

3 Answers3

24

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

}
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
4

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

@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)

herau
  • 1,466
  • 2
  • 18
  • 36
2

With small amount of configuration and one ImportBeanDefinitionRegistrar you can configure bean aliases via Java configuration. You can check bean-alias library 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.

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 ImportBeanDefinitionRegistrar implementation

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

which registers the alias in the 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);
    }
}
jankovd
  • 1,681
  • 1
  • 16
  • 22
  • I was able to do this with standard JavaConfig, by implementing BeanFactoryPostProcessor . See my answer: https://stackoverflow.com/questions/54938127/aliasing-a-bean-outside-the-bean-definition-using-java-config-in-spring-boot/61685677#61685677 – Scott Carlson May 08 '20 at 18:30