<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 抽象bean,abstract属性为true的bean不能被实例化,只用来被继承 -->
<bean id="address" class="com.dasenlin.test.springmodel.autowire.Address"
p:city="Beijing" p:street="WuKeSong" abstract="true"></bean>
<bean id="address2" p:city="ShangHai" parent="address" p:street="KunSa"></bean>
<!-- bean之间的依赖 ,我现在要求person必须依赖于某个Car,虽然不会直接将这个值赋予person,但是没哟car的话会报错-->
<bean id="car" class="com.dasenlin.test.springmodel.autowire.Car"
p:brand="Bmw" p:price="123000.00"></bean>
<bean id="person" class="com.dasenlin.test.springmodel.autowire.Person"
p:address-ref="address2" depends-on="car"></bean>
</beans>
java 部分
package com.dasenlin.test.springmodel.parent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dasenlin.test.springmodel.autowire.Address;
import com.dasenlin.test.springmodel.autowire.Person;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-relation.xml");
/*Address address = (Address) ctx.getBean("address");
System.out.println(address);*/
Address address = (Address) ctx.getBean("address2");
System.out.println(address);
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}
}