![](/img/trans.png)
[英]Spring and Abstract class - injecting properties in abstract classes
[英]problem with injecting abstract class in spring
我有两个抽象类
class abstract A {
//some methods .
}
class abstract B extends A {
private C c ;
//other methods
}
Spring配置文件:
<bean id="b" class="B" abstract="true">
<property name="c" ref="C" /> //I have reference for C else where
</bean>
当我运行程序时,类c没有注入。 它作为null来。 我想念什么吗?
abstract=true
表示Bean规范是其他Bean声明扩展的“模板”,并不意味着该类是抽象的。 我怀疑ID为b
bean未被创建,因为它是模板/抽象定义。 删除abstract=true
并将B
设为具体类型,它应该可以工作。
此处的文档: http : //static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-child-bean-definitions
尽管使用'abstract =“ true”'并不意味着该bean规范是针对抽象类的,但对于抽象类bean定义仍然需要它,这样就不会在该类上尝试预先实例化(这会失败的抽象类)。 以上链接指向的部分下方的注释中指出了这一点(http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-child-bean-definitions)。 如果在这种情况下,超类不是抽象类,则可以,不应使用'abstract =“ true”'。
您不会在抽象类B中显示C的二传手。您必须使用设置或构造函数注入。 发布的代码无法正常工作。
您还应该将B指定为C的父bean。 同样,A代表B。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.