Spring抛出错误:Bean定义是抽象的 [英] Spring throws the error: Bean definition is abstract

查看:318
本文介绍了Spring抛出错误:Bean定义是抽象的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于下一个主题的帮助。



我有一个bean的层次结构,就像那样:

 < bean id =fatherclass =myPackage.Fatherabstract =true/> 

< bean id =childAclass =myPackage.ChildAparent =father>
< property name =atrib1value =hello>< / property>
< / bean>

< bean id =childBclass =myPackage.ChildBparent =father>
< property name =atrib2value =bye>< / property>
< property name =atrib3value =other>< / property>
< / bean>

我需要创建另一个包含对抽象类的引用的bean。然后在运行时,我需要能够初始化TYPEmyPackage.Father的变量,并使用任何子类(多态机制)实例化它。我的意思是这样的:

 < bean id =referenceclass =myPackage.Another> 
< property name =atrib4ref =father>< / property>
< / bean>

请注意,引用(myPackage.Another的实例)不是myPackage.Father的子类。但是使用这些行Spring会抛出下一个错误:

  Bean定义是抽象的

我怎么处理它?<​​/ p>

我感谢任何帮助,提前谢谢!

解决方案

首先,抽象bean与抽象类
无关,它们甚至不需要映射到一个类。它们用于对另一个bean共享的公共属性进行分组。但是你不能使用抽象bean作为引用来连接另一个bean中的属性。
如果你的bean myPackage.Another有一个类型为myPackage.Father的引用,你的子bean可以赋值给myPackage.Father,那么你可以在myPackage中连接childA或childB
。像这样的另一个bean

 < bean id =referenceclass =myPackage.Another> 
< property name =atrib4ref =childA>< / property>
< / bean>


I need some help with next topic.

I have a hierarchy of beans, just like that:

<bean id="father" class="myPackage.Father" abstract="true"/>  

<bean id="childA" class="myPackage.ChildA" parent="father">    
        <property name="atrib1" value="hello"></property>  
</bean>  

<bean id="childB" class="myPackage.ChildB" parent="father">  
        <property name="atrib2" value="bye"></property>  
        <property name="atrib3" value="other"></property>  
</bean>  

I need to create another bean which contains a reference to the abstract class. Then at run-time, I will need to be able to initialize a variable of TYPE "myPackage.Father" and instantiate it with any of the children classes (the polymorphism mechanism). I mean something like that:

<bean id="reference" class="myPackage.Another"> 
        <property name="atrib4" ref="father"></property>
</bean>  

Note that reference (an instance of myPackage.Another) is not a subclass of myPackage.Father. But with these lines Spring throws the next error:

Bean definition is abstract

How could I deal with it?

I appreciate any help, thanks in advance!

解决方案

First of all abstract bean has nothing to do with the abstract class they don't even need to be mapped to a class.They are used to group the common properties shared by another beans.But you can not use an abstract bean as a ref to wire a property in another bean. If your bean myPackage.Another has a reference of type myPackage.Father and your child beans are assignable (either extends of implements) to myPackage.Father then you can wire either childA or childB in your myPackage.Another bean like this

 <bean id="reference" class="myPackage.Another"> 
            <property name="atrib4" ref="childA"></property>
    </bean>

这篇关于Spring抛出错误:Bean定义是抽象的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆