Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring support in IDEA with Lombok: Is "Navigate to autowired dependencies" supported?

Lombok supports to generate constructors with @Inject annotations:

@RequiredArgsConstructor(onConstructor = @__(@Inject))

So, instead of

@Service
public class FooService {

    private final BarService barService;

    @Inject
    public FooService(BarService barService) {
        this.barService = barService;
    }
}

you can write

@Service
@RequiredArgsConstructor(onConstructor = @__(@Inject))
public class FooService {
    private final BarService barService;
}

My problem with that feature is that it seems to break Spring IDE support in IntelliJ:

  • In the non-Lombok version, the IDE shows my where the autowired arguments come from and allows me to navigate to their locations (here, the implementation of the BarService).
  • In the Lombok version, I cannot get it to work. It still shows the location of the spring bean declaration, but not to the location of the autowired dependencies.

If I was the only developer, I would just use the first version and be done with it. However, I see that more and more code in our codebase is migrated to the Lombok constructor style. So, I am curious:

Is it possible to use the full Spring support in IDEA in combination with Lombok onConstructor = @__(@Inject) constructors?

like image 887
Philipp Claßen Avatar asked Jan 19 '26 07:01

Philipp Claßen


1 Answers

The lombok annotation @RequiredArgsConstructor will do the constructor based dependency injection.

@Service
@RequiredArgsConstructor
public class FooService {
    private final BarService barService;
}

After Java 17 record will also do this like -

@Service
public record FooService(BarService barService) {
}
like image 105
Santanu Avatar answered Jan 21 '26 23:01

Santanu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!