Spring + Lombok + @Qualifier or injection just became a bit easier (Part 2 of 2)

How to make Lombok copy @Qualifier on generated constructors for a Spring Component/Service

In the previous part of my blogpost I mentioned that it is now possible to actually add @Qualifier to a field in Spring-wired class and it’d be properly wired. Without further ado:

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

Due to the new copyableAnnotations feature introduced by lombok v1.18.4,  all you need to achieve this is to add this line in your lombok.config file which you can place in the root of your 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

view raw
lombok.config
hosted with ❤ by GitHub

Lombok already copies some specific annotations like NonNullNullable, or CheckForNull but now you can tweak that by appending to the list of annotations you want lombok to copy to the constructor.

One of them, of course, being the

org.springframework.beans.factory.annotation.Qualifier

annotation.

SilentDigitalIriomotecat-size_restricted

I hope this helps you guys into writing cleaner, better, more idiomatic Java code. Happy coding!

 

Extra:

As I feel instrumental to this feature, here is a little backstory:

Back in 2015 we saw this feature is the only thing preventing us from writing clear, idiomatic @RequiredArgsConstructor code in all our repos. Luckily, the same question has already been asked on StackOverflow and a ticket existed in lombok. However, in the beginning, it was more of a question and less of a feature, I felt like I needed to (like in my teenage years) get things in my own hands (yes, I make this kind of jokes…):

proposal.png

original link to my suggestion

That’s where the beauty of the crowd and open source projects lies in; gain enough traction, and you can deeply influence a project! Many many thanks to Roel Spilker @rspilker from Lombok for implementing my suggestion and I hope we’ve made it easier for all the community to write (and read) cleaner code!

 

3 thoughts on “Spring + Lombok + @Qualifier or injection just became a bit easier (Part 2 of 2)

  1. Hi

    Thanks for this great feature. Also, I think we need a moveableAnnotations because when we use copyableAnnotations, it does not remove the annotation from the field itself. When I want to use it for Spring @Value annotation, it’s not useful. Because in the output class still, I have the @Value for parameter and for the constructor.

    This is what I have:

    class Test {
    @Value({“conf.name”}) private final String prop1;

    public Test(@Value({“conf.name”}) String prop1) {
    this.prop1 = prop1;
    }
    }

    and this is what I need:

    class Test {
    private final String prop1;

    public Test(@Value({“conf.name”}) String prop1) {
    this.prop1 = prop1;
    }
    }

    1. Hey, maybe it can be a part of the copyableAnotations, but a parameter like: keepOnField, per class.

      for example (pseudocode):

      copyableAnnotations={@Value, false}, {@Qualifier, true}

      Something like that’d be good?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.