Spring Top – Temp

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

Lightrun – Third Party Code

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

1. Introduction

In this quick tutorial, we'll explain how to use the @Autowired annotation in abstract classes.

We'll apply @Autowired to an abstract class, and focus on the important points which we should take into account.

2. Setter Injection

We can use @Autowired on a setter method:

public abstract class BallService {

    private LogRepository logRepository;

    public final void setLogRepository(LogRepository logRepository) {
        this.logRepository = logRepository;

When we use @Autowired on a setter method, we should use the final keyword, so that the subclass can't override the setter method. Otherwise, the annotation won't work as we expect.

3. Constructor Injection

We can't use @Autowired on a constructor of an abstract class.

Spring doesn't evaluate the @Autowired annotation on a constructor of an abstract class. The subclass should provide the necessary arguments to the super constructor.

Instead, we should use @Autowired on the constructor of the subclass:

public abstract class BallService {

    private RuleRepository ruleRepository;

    public BallService(RuleRepository ruleRepository) {
        this.ruleRepository = ruleRepository;
public class BasketballService extends BallService {

    public BasketballService(RuleRepository ruleRepository) {

4. Cheat Sheet

Let's just wrap up with a few rules to remember.

First, an abstract class isn't component-scanned since it can't be instantiated without a concrete subclass.

Second, setter injection is possible in an abstract class, but it's risky if we don't use the final keyword for the setter method. The application may not be stable if a subclass overrides the setter method.

Third, as Spring doesn't support constructor injection in an abstract class, we should generally let the concrete subclasses provide the constructor arguments. This means that we need to rely on constructor injection in concrete subclasses.

And finally, using constructor injection for required dependencies and setter injection for optional dependencies is a good rule of thumb. However, as we can see with some of the nuances with abstract classes, constructor injection is more favorable here in general.

So, really we can say that a concrete subclass governs how its abstract parent gets its dependencies. Spring will do the injection as long as Spring wires up the subclass.

5. Conclusion

In this article, we practiced using @Autowired within an abstract class and explained a few but important key points.

The source code of this tutorial can be found in the Github project as usual.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Comments are closed on this article!