-1

Is it possible to pass a parameter to the @Qualifier("passedParameter") when using spring with classes that implement the same interface.

so an interface that is say Customer

then three classes that implement Customer are CustomerA CustomerB and CustomerC

then a class that autowires the Person interface and I want to specify the particular class I want by passing a parameter into @Qualifier("CustomerA") ?

Adi
  • 2,364
  • 1
  • 17
  • 23
  • possible duplicate of [Spring Qualifier and property placeholder](http://stackoverflow.com/questions/7812745/spring-qualifier-and-property-placeholder) – jny Sep 02 '14 at 18:23

2 Answers2

0

@Qualifier should be used with @Autowired annotation to pass bean name if there is more than one bean of single interface. spring autowire bydefault look to resolve by type, so you need to add qualifier with the name of bean name which you have defined at CustomerA using qualifier. Also you can use @Resource annotation which does the job of both autowird and qualifier and takes string argument as name of bean you want to wire..

Developer G
  • 11
  • 1
  • 5
  • So can I add it dynamically at runtime, so in essence I won't the actual class i want until say CustomerA logs in or CustomerB or CustomerC and which ever one does I want that class to be called? – Jonathan Hollick Sep 03 '14 at 12:54
  • @Autowire by default buid dependencies and wire at startup, if you want to lazily initialise try to add attribute required=false. Is this which you are looking for? – Developer G Sep 03 '14 at 13:13
  • Hi thanks for getting back, to me. I guess what I am trying to say is I will know that a particular type of customer is using the system so I want to say dynamically that TrustCustomer Acme logs in to the system I will want it to use TrustCustomer Acme class, and I will only know at run time? – Jonathan Hollick Sep 04 '14 at 08:10
0

Its always best to use @Resource("CustomerA"), @Resource("CustomerB") like that. Using @Autowired and Qualifier together when you have multiple implementations of the same interface messes up the code and instead name your beans and use @Resource("name") to inject them.

Please read this article about various styles http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/ .

ROCKY
  • 1,763
  • 1
  • 21
  • 25