0

I have a project where I use heavily autowiring, e.g.

@Component("componentA")
public class ComponentAImpl implements ComponentA

@Component("componentB")
public class ComponentBImpl implements ComponentB {
   @Autowired
   private ComponentA componentA;
}

But I want customer to extend all the classes, so ideally they would just add a single jar into the classpath the the extended class should be used instead of the original one. What I think would be great if they could just set a different name for the bean, e.g.

@component("componentA-custom)

I just need some way to customize the autowire process to look for a bean with "-custom" at the end and load it instead of the original bean. Is there any way to do that?

Any help appreciated. Thank you, Mariusz

Mariusz Pala
  • 961
  • 1
  • 8
  • 17

3 Answers3

1

I found a solution, there is

@Primary

annotation that can be used to specify that the custom code added should be loaded instead of the original code.

Mariusz Pala
  • 961
  • 1
  • 8
  • 17
0

What you are looking for is something similiar to this.
This should help you solve your problem. If you need to get the bean name, you can inject in an instance of applicationContext which will get you the correct bean name (through a reverse lookup). or force all beans (ComponentA and CustomImplementation) to implement BeanNameAware

Community
  • 1
  • 1
AdityaKeyal
  • 1,208
  • 8
  • 14
0

What You are asking about can be done via @Qualifier annotation, but it cannot generate bean names dynamically.

To allow external beans, You will need to create XML configuration for this, as it overrides annotation - based configuration. Your customers would have to provide:

  1. New beans
  2. XML configuration for your application that uses the new beans.
endriu_l
  • 1,649
  • 16
  • 20
  • I have my class annotated with `@Component("componentA")` and I want to let customers to create a custom implementation annotated with `@Component("componentA-custom")` that should be picked up during the auto-wiring process. So basically I need to customize the way the class is found by name to look first by a different name and if not found - use the default mechanism. – Mariusz Pala Apr 25 '14 at 10:49