spring-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: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);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值