如何将 class 设置为 bean 中的值?
How do i set a class as a value in beans?
我正在用豆子做我的第一步,我想给学生一个宿舍,也是一个class。我做错了什么?
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`
我已经尝试 google 或通过转换修复它,但我的知识有限。所以请帮助我。
如果要引用另一个 Spring bean,请使用属性 ref:
<property name="hostel" ref="aushos"/>
Hostel在这里是一个bean,所以应该引用而不是复制。所以使用
<property name="hostel" ref="aushos"/>
"ref"属性是引用bean,value属性是复制原始类型值。
我正在用豆子做我的第一步,我想给学生一个宿舍,也是一个class。我做错了什么?
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`
我已经尝试 google 或通过转换修复它,但我的知识有限。所以请帮助我。
如果要引用另一个 Spring bean,请使用属性 ref:
<property name="hostel" ref="aushos"/>
Hostel在这里是一个bean,所以应该引用而不是复制。所以使用
<property name="hostel" ref="aushos"/>
"ref"属性是引用bean,value属性是复制原始类型值。