2015-10-20 50 views
-1

我正在做豆子的第一步,我想给学生一个宿舍,这也是一个班。我究竟做错了什么?我如何将类设置为bean中的值?

public class Student { 
private String name; 
private String id; 
private Hostel hostel; 
//getters and setters under this 

public class Hostel { 
private String hostelName; 
private String city; 
//also getters and setters 

beanpart

<bean id="student" class="com.tom.demo.Student" autowire="byName"> 
    <property name="name" value="Sönke Huster"></property> 
    <property name="id" value="s81862322B"></property> 
</bean> 

<bean id="student1" class="com.tom.demo.Student"> 
    <property name="name" value="Thomas Bruhn"></property> 
    <property name="id" value="s8232322"></property> 
    <property name="hostel" value="aushos"/> 

</bean> 

<bean id="hostel" class="com.tom.demo.Hostel"> 
    <property name="hostelName" value ="Bangladore East Hostel"></property> 
    <property name="city" value="Bangladore"></property> 
</bean> 

<bean id="aushos" class="com.tom.demo.Hostel"> 
    <property name="hostelName" value ="Easy Go Backpackers"></property> 
    <property name="city" value="Sydney"></property> 
</bean> 

我的结果是:

Failed to convert property value of type `java.lang.String` to required type `com.tom.demo.Hostel` for property `hostel`; nested exception is `java.lang.IllegalStateException` 

我已经尝试过谷歌或与铸造修复它,但我的知识是有限的。所以请帮助我。

+0

我想你必须使用'ref = ....'引用其他bean。 –

+0

很好,谢谢 –

回答

0

使用属性裁判,如果要引用到另一个的Spring bean:

<property name="hostel" ref="aushos"/> 
0

宿舍是一个bean这里,所以它应该被称为,而不是复制。所以使用 <property name="hostel" ref="aushos"/> “ref”属性是指bean和value属性是复制原始类型的值。

相关问题