XML 代码:
<!-- 使用构造器注入属性值的位置和参数的类型!以区分重载的构造器! -->
<bean id="car1" class="com.hy.spring.beans.Car">
<constructor-arg value="BaoMa" type="String"></constructor-arg>
<!--如果字面值包含特殊字符可以使用 <![CDATA[]]> 包裹起来 -->
<!--属性值也可以使用value 直接点进行配置 -->
<constructor-arg type="String">
<value> <![CDATA[<China^>]]> </value>
</constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
</bean>
Person.java
package com.hy.spring.beans;
public class Person {
private String name;
private int age;
private Car car;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
XML 代码
<!-- 使用构造器注入属性值的位置和参数的类型!以区分重载的构造器! -->
<bean id="car1" class="com.hy.spring.beans.Car">
<constructor-arg value="BaoMa" type="String"></constructor-arg>
<!--如果字面值包含特殊字符可以使用 <![CDATA[]]> 包裹起来 -->
<!--属性值也可以使用value 直接点进行配置 -->
<constructor-arg type="String">
<value> <![CDATA[<China^>]]> </value>
</constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
</bean>
<bean id="person" class="com.hy.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="27"></property>
<!-- 可以使用 property 的 ref 属性建立bean之间的引用关系-->
<property name="car" ref="car1"></property>
</bean>
XML 配置
<bean id="person" class="com.hy.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="27"></property>
<!-- 可以使用 property 的 ref 属性建立bean之间的引用关系-->
<!-- <property name="car" ref="car1"></property> -->
<!-- 内部Bean,不能被外部引用,只能在内部使用 -->
<property name="car">
<bean class="com.hy.spring.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="ChangAn"></constructor-arg>
<constructor-arg value="300000" type="double"></constructor-arg>
</bean>
</property>
</bean>
<bean id="person1" class="com.hy.spring.beans.Person">
<constructor-arg name="name" value="Jack"></constructor-arg>
<constructor-arg name="age" value="35"></constructor-arg>
<constructor-arg name="car">
<bean class="com.hy.spring.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="ChangAn"></constructor-arg>
<constructor-arg value="300000" type="double"></constructor-arg>
</bean>
</constructor-arg>
</bean>
<bean id="person2" class="com.hy.spring.beans.Person">
<constructor-arg name="name" value="Hao"></constructor-arg>
<constructor-arg name="age" value="30"></constructor-arg>
<constructor-arg name="car" ref="car"></constructor-arg>
<!-- 为级联属性赋值。
注意:属性要先初始化后才可以为级联属性赋值,否则会出异常,
和sturct2 不同。 -->
<property name="car.maxSpeed" value="250"></property>
</bean>
Person.java
package com.hy.spring.beans;
public class Person {
private String name;
private int age;
private Car car;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
}
public Person(String name, int age, Car car) {
super();
this.name = name;
this.age = age;
this.car = car;
}
public Person (){
}
}