bean之间的属性是怎么维护的

spring对象【bean】之间的属性是通过什么维护的:

    1、构造方法:标签:constructor-arg

    2、set方法:标签:property

<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-4.0.xsd">



<!--
标签"高亮"取决与上面的配置,该内容可作为模版使用
-->




<bean id="user" class="Cristin.Spring.User">
<!--<property name="name" value="杨晓龙"></property> -->
<!--<property name="age" value="18"></property>-->
<!--
注意:
1、value单独拉出来写的时候,注意类型,不需要引号
2、name对应方法里的属性
-->
<!--<property name="age">
<value>18</value>
</property>
<property name="car" ref="carId"></property>
-->
<!--
构造器,如果User类存在带参数的新的构造方法,那么就需要构造器来对构造方法进行赋值,类的执行中,是先编译构造方法后再执行get、set,
因此,该场景配置下 包蕾 19 数据会被后来的杨晓龙 18 所覆盖
-->


<constructor-arg index="0"><value>包蕾</value></constructor-arg>
<constructor-arg index="1"><value>19</value></constructor-arg>


<!--<constructor-arg index="2" ref="carId"></constructor-arg>-->

<constructor-arg index="2">
<ref bean="carId"></ref>
</constructor-arg>
<property name="carList">
<list>
<!--简单的写法:直接通过ref引用外面的,ref表示引用哪个对象-->
<ref bean="carId"></ref>
<ref bean="carId"></ref>
<!--复杂的写法:自己直接插数据-->
<bean id="newcar" class="Cristin.Spring.Car">
<property name="brand" value="奔驰"></property>
<property name="price" value="500000"></property>
</bean>
</list>
</property>

<property name="carSet">
<set>
<!--简单的写法:直接通过ref引用外面的,ref表示引用哪个对象-->
<ref bean="carId"></ref>
<ref bean="carId"></ref>
<!--复杂的写法:自己直接插数据-->
<bean id="newcar" class="Cristin.Spring.Car">
<property name="brand" value="奔驰"></property>
<property name="price" value="500000"></property>
</bean>
</set>
</property>

<property name="carMap">
<map>
<entry key="name" value="杨子昫"></entry>
<entry key="age" value="18"></entry>
<entry key="car">
<bean class="Cristin.Spring.Car">
<property name="price" value="130000"></property>
<property name="brand" value="雪弗兰"></property>
</bean>
</entry>
<entry key="car2" value-ref="carId"></entry>
</map>
</property>
</bean>


<bean id="carId" class="Cristin.Spring.Car">
<property name="brand" value="大众"></property>
<property name="price" value="1800000"></property>
</bean>

</beans>
原文地址:https://www.cnblogs.com/cristin/p/7667117.html