當一個bean僅被用做另外一個bean的屬性時,它能被聲明爲一個內部bean,爲了定義inner bean,在Spring 的 基於XML的 配置元數據中,能夠在 <property/>或 <constructor-arg/> 元素內使用<bean/> 元素,內部bean一般是匿名的,它們的Scope通常是prototype。spring
<?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.xsd"> <bean id="person2" class="com.itdjx.spring.dependency.injection.Person"> <property name="name" value="李玉"/> <property name="age" value="23"/> <property name="sex" value="女"/> <property name="car" > <bean class="com.itdjx.spring.dependency.injection.Car"> <constructor-arg value="Ferrari" index="0"/> <constructor-arg value="Italy" index="1"/> <constructor-arg value="22500000" type="double"/> </bean> </property> </bean> </beans>