一、 前言
平常我们在使用spring框架开发项目过程中,会使用@Autowired
注解进行属性依赖注入,一般我们都是声明接口类型来接收接口实现变量,那么使用父类类型接收子类变量,可以注入成功吗?答案是肯定可以的!
二、结果验证
我们在项目中声明如下三个类:
1. 测试代码
- TestParent
public class TestParent {
protected void test() {
System.out.println("I am TestParent...");
}
}
- TestSon
importorg.springframework.stereotype.Component;
@Component
public class TestSon extends TestParent {
publicvoidtest() {
System.out.println("I am TestSon...");
}
}
- TestType
importorg.springframework.beans