类
package com.test.innerbean;
import java.util.Date;
public class Goods {
private String goodsName;
private int price;
private Date date;
public Goods(String name,int price,Date date)
{
goodsName=name;
this.price=price;
this.date=date;
}
public void say()
{
System.out.println("goodsname:"+goodsName+" price:"+price+" date:"+date);
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="goods" class="com.test.innerbean.Goods">
<span style="white-space:pre"> </span><constructor-arg><value>电脑</value></constructor-arg>
<span style="white-space:pre"> </span><constructor-arg><value>13</value></constructor-arg>
<span style="white-space:pre"> </span><constructor-arg><ref bean="date"/></constructor-arg>
</bean>
<bean id="date" class="java.util.Date"></bean>
</beans>
主要注意的就是这两个文件,注意在构造函数中,如何注入对象
或者也可以使用内部bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="goods" class="com.test.innerbean.Goods">
<constructor-arg><value>电脑</value></constructor-arg>
<constructor-arg><value>13</value></constructor-arg>
<constructor-arg><bean id="date" class="java.util.Date"></bean> </constructor-arg>
</bean>
</beans>