我有一个父抽象类和一个具体的子类。抽象的有一个具体方法和另一个抽象方法,子类实现。这两个类都配置为 Spring bean。代码片段类似于以下内容:
弹簧配置:
<bean id="parent" class="Parent" abstract="true">
<property name="propA" ref="propA"/>
<property name="cacheMan" ref="cacheMan"/>
</bean>
<bean id="child" class="Child" parent="parent">
<property name="propB" ref="propB"/>
<property name="cacheMan" ref="cacheMan"/>
</bean>
<!-- Of course, cacheMan is defined elsewhere and not relevant here.-->
类定义:
public abstract class Parent {
private A propA; // A is irrelevant.
private CacheManager cacheMan; // This is the object in question.
public void doProcess1() {
//code logic
if (cacheMan == null) {
// Error!
}
}
public void doProcess2();
}
public class Child extend Parent {
private B propB; // Again, B is irrelevant.
private CacheManager cacheMan; // This is the object in question.
public void doProcess2() {
//code logic
}
}
public class Test {
private Parent parent; //Assume this is Spring injected and it's of type Child.
public void doTest() {
parent.doProcess1(); // Error thrown since cacheMan is null.
}
}
这两个类都有适当的 getter/setter 方法cacheMan
。我不明白cacheMan
该方法中的空值如何doProcess1()
。但是,如果我从
cacheMan == null
to getCacheMan() == null
,不抛出错误。
我认为getCacheMan()
正在检索注入到子类的对象,因为parent
它是实例Child
,这就是它不为空的原因。
请对此有所了解,如果不清楚,请告诉我。