Java 如何在bean中将类设置为值?

Java 如何在bean中将类设置为值?,java,spring,javabeans,Java,Spring,Javabeans,我正在用豆子做我的第一步,我想给学生一个宿舍,这也是一个班级。我做错了什么 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 set

我正在用豆子做我的第一步,我想给学生一个宿舍,这也是一个班级。我做错了什么

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>

我已经试着用谷歌搜索或者用铸造来修复它,但是我的知识有限。因此,请帮助我。

如果您想引用另一个Springbean,请使用属性ref:

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

这里的hotel是一个bean,所以应该引用它,而不是复制它。所以使用

“ref”属性用于引用bean,value属性用于复制原语类型值。

我想您必须使用
ref=..
来引用其他bean。
<property name="hostel" ref="aushos"/>