<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="address" p:city="BeiJing^" p:street="WuDaoKou" class="com.lijunyang.model.Address" /><bean id="address2" p:street="DaZhongSi" parent="address" /></beans>
继承Bean配置
- Spring允许继承bean的配置,被继承的bean称之为父bean,继承这个父Bean的Bean称为子Bean
- 子Bean从父Bean中继承配置,包括Bean的属性配置
- 子Bean也可以覆盖从父Bean继承过来的配置
- 父Bean作为模版,可以设置
的abstract属性为true,这样Spring将不会实例化这个Bean - 并不是
元素里所有的属性都会被继承,比如:autowire,abstract等 也可以忽略父Bean的class属性,让子Bean指定自己的类,而共享相同的属性配置,但此时父Bean abstract必须设为true
依赖Bean配置
Spring 允许用户通过depends-on属性设定Bean前置依赖的Bean,前置依赖的Bean会在本Bean实例化之前创建好
- 如果前置依赖于多个Bean,则可以通过逗号,空格或的方式配置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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="address" p:city="BeiJing^" p:street="WuDaoKou" class="com.lijunyang.model.Address" /><bean id="address2" p:street="DaZhongSi" parent="address" />// 此时如果没有car,则程序抛出异常<bean id="preson" p:name="Tom" p:address-ref="address" depends-on="car" />// 有了car,则上面的preson就不会报错了,但是因为没有p:car-ref="car"// ,所以上面实例化的bean没有car对象<bean id="car" class="com.lijunyang.model.Car" p:brand-ref="Audi" p:price-ref="300000" /></beans>
