Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add qualifiers in @RequiredArgsConstructor(onConstructor = @__(@Autowired))?

If I wanted to use the annotation @Qualifier on a constructor dependency injection, I would have something like the following:

public class Example {      private final ComponentExample component;      @Autowired     public Example(@Qualifier("someComponent") ComponentExample component) {         this.component = component;     } } 

I know Lombok's annotations to reduce boilerplate code and don't have to include a constructor would be as follows: @RequiredArgsConstructors(onConstructor=@__(@Inject)) but this only works with properties without qualifiers.

Anyone know if it is possible to add qualifiers in @RequiredArgsConstructor(onConstructor = @__(@Autowired))?

like image 299
Pau Avatar asked Jul 24 '16 07:07

Pau


People also ask

Can we use qualifier with Autowired?

The @Qualifier annotation is used to resolve the autowiring conflict, when there are multiple beans of same type. The @Qualifier annotation can be used on any class annotated with @Component or on methods annotated with @Bean . This annotation can also be applied on constructor arguments or method parameters.

What is difference between qualifier and Autowired?

The difference are that @Autowired and @Qualifier are the spring annotation while @Resource is the standard java annotation (from JSR-250) . Besides , @Resource only supports for fields and setter injection while @Autowired supports fields , setter ,constructors and multi-argument methods injection.

Can we use @qualifier with @bean?

NOTE: if you are creating bean with @Bean, it will be injected byType if there is duplicates then it will injected byName. we no need to mention @Bean(name="bmwDriver") . so you can directly use qualifier("bmwDriver") wherever you need in classes.

How do you use qualifier annotations?

The @Qualifier annotation in Spring is used to differentiate a bean among the same type of bean objects. If we have more than one bean of the same type and want to wire only one of them then use the @Qualifier annotation along with @Autowired to specify which exact bean will be wired.


2 Answers

EDIT:

It is FINALLY POSSIBLE to do so! You can have a service defined like this:

@Service @RequiredArgsConstructor public class SomeRouterService {     @NonNull private final DispatcherService dispatcherService;    @Qualifier("someDestination1") @NonNull private final SomeDestination someDestination1;    @Qualifier("someDestination2") @NonNull private final SomeDestination someDestination2;     public void onMessage(Message message) {        //..some code to route stuff based on something to either destination1 or destination2    }   }  

Provided that you have a lombok.config file like this in the root of the project:

# Copy the Qualifier annotation from the instance variables to the constructor # see https://github.com/rzwitserloot/lombok/issues/745 lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier 

This was recently introduced in latest lombok 1.18.4, I wrote about it in my blogpost, and I am proud to say I was one of the main driving forces pushing for the implementation of the feature.

  • The blog post where the issue is discussed in detail
  • The original issue on github
  • And a small github project to see it in action
like image 196
Nikola Yovchev Avatar answered Sep 19 '22 11:09

Nikola Yovchev


You may use spring trick to qualify field by naming it with desired qualifier without @Qualifier annotation.

@RequiredArgsConstructor public class ValidationController {    //@Qualifier("xmlFormValidator")     private final Validator xmlFormValidator; 
like image 20
Алексей Виноградов Avatar answered Sep 21 '22 11:09

Алексей Виноградов