[英]problem with injecting abstract class in spring
I have two abstract classes 我有两个抽象类
class abstract A {
//some methods .
}
class abstract B extends A {
private C c ;
//other methods
}
Spring config file : Spring配置文件:
<bean id="b" class="B" abstract="true">
<property name="c" ref="C" /> //I have reference for C else where
</bean>
When I run the program , the class c is not getting injected . 当我运行程序时,类c没有注入。 It is coming as null . 它作为null来。 Am i missing something ? 我想念什么吗?
abstract=true
means that the bean specification is a 'template' for other bean declarations to extend, it does not mean the class is abstract. abstract=true
表示Bean规范是其他Bean声明扩展的“模板”,并不意味着该类是抽象的。 I suspect bean with id b
is not being created since it is a template/abstract definition. 我怀疑ID为b
bean未被创建,因为它是模板/抽象定义。 Remove abstract=true
and make B
a concrete type and it should work. 删除abstract=true
并将B
设为具体类型,它应该可以工作。
Documentation here: http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-child-bean-definitions 此处的文档: http : //static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-child-bean-definitions
Although the use of 'abstract="true"' is not meant to indicate that the bean specification is for an abstract class, it is still required for an abstract class bean definition so that pre-instantiation is not attempted on that class (which would fail for an abstract class). 尽管使用'abstract =“ true”'并不意味着该bean规范是针对抽象类的,但对于抽象类bean定义仍然需要它,这样就不会在该类上尝试预先实例化(这会失败的抽象类)。 This is indicated in a note below the section that the above link points to (http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-child-bean-definitions). 以上链接指向的部分下方的注释中指出了这一点(http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-child-bean-definitions)。 If this were a situation where the super class was not an abstract class, then yes, 'abstract="true"' should not be used. 如果在这种情况下,超类不是抽象类,则可以,不应使用'abstract =“ true”'。
You don't show a setter for C in the abstract class B. You must use either setting or constructor injection. 您不会在抽象类B中显示C的二传手。您必须使用设置或构造函数注入。 The code as posted can't work. 发布的代码无法正常工作。
You should also specify B as the parent bean for C; 您还应该将B指定为C的父bean。 likewise A for B. 同样,A代表B。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.