标签:style blog http java color 使用
概要:
‘
继承Bean配置
依赖Bean配置
package com.coslay.beans.relation; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.coslay.beans.autowire.Address; import com.coslay.beans.autowire.Person; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml"); Address address = (Address) ctx.getBean("address3"); System.out.println(address); address = (Address) ctx.getBean("address2"); System.out.println(address); Person person = (Person) ctx.getBean("person"); System.out.println(person); } }
<?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:bean的abstract属性为true的bean。这样bean不能被IOC容器实例化,仅仅能用来被继承继承 若某一个bean的class属性没有指定,则该bean必须是一个抽象bean--> <bean id="address" p:city="Beijing" p:street="WuDaoKou" abstract="true"></bean> <!-- <bean id="address2" class="com.coslay.beans.autowire.Address" p:city="Beijing" p:street="DaZhongSi"></bean> --> <!-- bean 配置的继承:使用bean 的parent 属性指定继承哪个baen的配置 --> <bean id="address2" class="com.coslay.beans.autowire.Address" parent="address"></bean> <bean id="address3" class="com.coslay.beans.autowire.Address" p:street="DaZhongSi" parent="address2"></bean> <bean id="car" class="com.coslay.beans.autowire.Car" p:brand="Audi" p:price="300000"></bean> <!-- 要求在配置Person时,必须有一个关联的car!换句话说person这个bean依赖于Car这个bean --> <bean id="person" class="com.coslay.beans.autowire.Person" p:name="Tom" p:address-ref="address2" depends-on="car"></bean> </beans>
spring bean之间的关系:继承;依赖,布布扣,bubuko.com
标签:style blog http java color 使用
原文地址:http://www.cnblogs.com/mfrbuaa/p/3844992.html