1. Introduction
1.绪论
In this quick tutorial, we’ll explain how to use the @Autowired annotation in abstract classes.
在这个快速教程中,我们将解释如何在@Autowired注解中使用抽象类>。
We’ll apply @Autowired to an abstract class, and focus on the important points which we should take into account.
我们将把@Autowired应用到一个抽象类中,并关注我们应该考虑到的重要问题。
2. Setter Injection
2.注入设定器
We can use @Autowired on a setter method:
我们可以在一个setter方法上使用@Autowired。
public abstract class BallService {
private LogRepository logRepository;
@Autowired
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.
当我们在setter方法上使用@Autowired时,我们应该使用final关键字,这样子类就不能重写setter方法。否则,注解就不会像我们期望的那样工作。
3. Constructor Injection
3.构造函数注入
We can’t use @Autowired on a constructor of an abstract class.
我们不能在一个抽象类的构造函数上使用@Autowired。
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.
Spring不会在抽象类的构造函数上评估@Autowired注解。子类应该向super构造函数提供必要的参数。
Instead, we should use @Autowired on the constructor of the subclass:
相反,我们应该在子类的构造函数上使用@Autowired。
public abstract class BallService {
private RuleRepository ruleRepository;
public BallService(RuleRepository ruleRepository) {
this.ruleRepository = ruleRepository;
}
}
@Component
public class BasketballService extends BallService {
@Autowired
public BasketballService(RuleRepository ruleRepository) {
super(ruleRepository);
}
}
4. Cheat Sheet
4.小抄
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.
其次,在抽象类中可以进行setter注入,但是如果我们不对setter方法使用final关键字,就会有风险。如果一个子类重写了setter方法,应用程序可能就不稳定了。
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.
第三,由于Spring不支持抽象类中的构造函数注入,我们一般应该让具体子类提供构造函数参数。这意味着我们需要在具体子类中依赖构造函数注入。
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.
因此,实际上我们可以说,具体的子类管理其抽象的父类如何获得其依赖性。只要Spring将子类连接起来,Spring就会进行注入。
5. Conclusion
5.总结
In this article, we practiced using @Autowired within an abstract class and explained a few but important key points.
在这篇文章中,我们练习了在一个抽象类中使用@Autowired,并解释了几个但重要的关键点。
The source code of this tutorial can be found in the Github project as usual.
本教程的源代码可以照常在Github项目中找到。