bean的装配细节主要对这几个方面进行考虑:
1.value只中存在特殊符号,例如<> ""等,用<![CDATA[特殊的value值]]>
<bean id="myCar4" class="com.gp.demo.bean.Car">
<property name="name" value="保时捷"/>
<property name="brand" >
<value><![CDATA[<五彩黑>]]></value>
</property>
<property name="price" value="8230"/>
</bean>
2.value值存在null,用<null/>进行赋空值,idea高版本会自动处理空值,低版本如果没有处理会报错
<bean id="person4" class="com.gp.demo.bean.Emp">
<property name="id" value="0021"/>
<property name="name" value="李货"/>
<property name="age" value="79"/>
<property name="sex" value="男"/>
<property name="car" ><null/></property>
</bean>
3.使用级联属性赋值,其中car是Emp类中的getCar方法,brand是Car中的getBrand方法。通过级联属性赋值改变原来的值
<bean id="person5" class="com.gp.demo.bean.Emp">
<property name="id" value="0021"/>
<property name="name" value="李货"/>
<property name="age" value="79"/>
<property name="sex" value="男"/>
<property name="car" ref="myCar4" />
<property name="car.brand" value="红色"/>
<property name="car.price" value="19999"/>
</bean>
4.Bean引用List
@Data
@NoArgsConstructor
@AllArgsConstructor
public class EmpWithCars {
private String name;
private String sex;
private Integer age;
private List<Car> cars;
}
<bean id="person6" class="com.gp.demo.bean.EmpWithCars">
<property name="name" value="李司机"/>
<property name="age" value="179"/>
<property name="sex" value="男"/>
<property name="cars" >
<list>
<ref bean="myCar1"/>
<ref bean="myCar4"/>
</list>
</property>
</bean>
6.Bean引用Map,可以用map和entry标签进行组合解决
@Data
@NoArgsConstructor
@AllArgsConstructor
public class EmpMap {
private String name;
private String sex;
private Integer age;
private Map<String,Car> carMap;
}
<bean id="person7" class="com.gp.demo.bean.EmpMap">
<property name="name" value="李司机"/>
<property name="age" value="179"/>
<property name="sex" value="男"/>
<property name="carMap" >
<map>
<entry key="myCar1" value-ref="myCar1"/>
<entry key="myCar4" value-ref="myCar4"/>
</map>
</property>
</bean>
7.Bean引用Properties
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyProps {
private Properties properties;
}
<bean id="myProp1" class="com.gp.demo.bean.MyProps">
<property name="properties">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql:localhost:3306</prop>
<prop key="username">root</prop>
<prop key="password">123</prop>
</props>
</property>
</bean>