(三)配置bean之间的关系:继承、依赖

来源:互联网 发布:做数据库有前途吗 编辑:程序博客网 时间:2024/05/17 20:24

第一步:建三个类Address.java、Car.java 、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不能被IOC容器实例化,只用来被继承配置         若某一个bean的class属性没有指定,该bean必须是一个抽象bean    -->    <bean id="address" class="com.atguigu.spring.beans.autowire.Address"          p:city="Beijing" p:street="WuDaoKou" abstract="true"></bean>    <!--         <bean id="address2" class="com.atguigu.spring.beans.autowire.Address"        p:city="Beijing" p:street="DaZhongSi"></bean>     -->    <!--bean配置的继承:使用bean的parent属性指定继承哪个bean的配置  -->    <bean id="address2" parent="address"></bean>    <bean id="address3" p:street="DaZhongSi" parent="address2"></bean>     <!--         要求配置Person時,必須有一个关联的car,换句话说这个bean依赖于car这个bean         Spring允许用户通过depends-on属性设定Bean前置依赖bean,前置依赖bean会在本bean实例化前创建好     -->    <bean id="car" class="com.atguigu.spring.beans.autowire.Car"         p:brand="Audi" p:price="300"></bean>    <bean id="person" class="com.atguigu.spring.beans.autowire.Person"         p:name="Tom" p:address-ref="address3" depends-on="car">     </bean></beans>

第三步:运行main方法

package com.atguigu.spring.beans.relation;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.atguigu.spring.beans.autowire.Address;import com.atguigu.spring.beans.autowire.Person;/** *  * 配置bean的继承和依赖 * */public class Main {    public static void main(String[] args) {        ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-relation.xml");        Address address=(Address) ctx.getBean("address2");        System.out.println(address);         address=(Address) ctx.getBean("address3");        System.out.println(address);         Person person=(Person) ctx.getBean("person");        System.out.println(person);//Person [name=Tom, address=Address [city=Beijing, street=DaZhongSifangxinde], car=null]    }}

第四步:运行结果

Car's Constructor.....Address [city=Beijing, street=WuDaoKou]Address [city=Beijing, street=DaZhongSi]Person [name=Tom, address=Address [city=Beijing, street=DaZhongSi], car=null]
阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 皮鞋撑大了怎么办变小 鞋穿着走路掉跟怎么办 浅口帆布鞋大了怎么办 布鞋后鞋跟老掉怎么办 穿坡跟凉鞋容易打滑摔倒怎么办 运动鞋烂了个口怎么办 运动鞋的网烂了怎么办 运动鞋鞋一个一个高一个低怎么办 淘宝预售水果不发货怎么办 淘宝评价忘记晒图了怎么办 参加水果展没有实物怎么办 没做过运营面试怎么办 苹果5s16g内存满了怎么办 618天猫有活动淘宝没有怎么办 天猫国际买到假货怎么办 天猫恶意差评怎么办 天猫删除差评被扣分了怎么办 天猫收到差评怎么办 天猫给差评骚扰怎么办 天猫客户差评怎么办 天猫没法给差评怎么办 天猫上限购一件怎么办 拍下商品不发货怎么办 天猫红包过期了怎么办 买天猫店被中介骗了钱怎么办 速卖通假货纠纷怎么办 天猫新开店被恶意拍下怎么办 新开的天猫店没有生意怎么办 银行的支票丢了怎么办 天猫积分用光了怎么办 淘宝店没有无线流量怎么办 京东卖家不发货怎么办会自动打款 一件代发顾客如果退款怎么办 京东以前的账号怎么办 旺旺对话框订单页面变宽了怎么办 阿里巴巴店铺上传图片很模糊怎么办 天猫跨店优惠券用了退货怎么办 淘宝店铺没流量没访客怎么办 京东微信和Q端黑号了怎么办? 买不了运费险了怎么办 拼多多5天不发货怎么办