Spring内部bean和级联属性用法详解

1、内部bean

内部Bean和Java的匿名内部类相似,既没有名字,也不能被其他Bean引用,只能在声明处为外部Bean提供实例注入

配置文件

<bean id="boss2" class="com.gec.bean.Boss"> 
  <property name="bossName"><value>马老板</value> </property>
  <property name="car">
  		 <!--直接引用内部bean的写法,类似匿名内部类的写法--> 
		<bean class="com.gec.bean.Car">
 			<property name="brand"><value>QQ跑车 </value></property> 
			<property name="color"><value>绿色</value> </property> 
			<property name="price"><value>300000.00</value></property> 
		</bean>
 	</property>
 </bean>

2、级联属性

Spring支持级联属性的配置,Spring没有对级联属性的层级数进行限制,只要配置的Bean拥有对应于级联属性的类结构,就可以配置任意层级的级联属性

配置文件

<bean id="testCar" class="com.gec.bean.Car"/>
<bean id="boss3" class="com.gec.bean.Boss"> 
  <property name="car"> <ref bean="testCar"/>
  </property> <property name="bossName"><value>许老板</value></property>
  <!--级联属性赋值--> 
  <property name="car.brand"><value>奔驰</value> </property> 
  <property name="car.color"><value>黑色</value> </property>
  <property name="car.price"><value>5000000.00</value> </property> 
</bean>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。