您的位置:首页 > 编程语言 > Java开发

Spring中bean之间的依赖关系depends-on

2016-11-20 12:32 519 查看
1:xml

<?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"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<bean id="Address" class="com.study.autowire.Address"

p:city="北京" p:street="万里长城">

</bean>

<bean id="car" class="com.study.autowire.Car"

p:brand="奥迪" p:price="360000"></bean>

<bean id="person" class="com.study.autowire.Person"

p:name="Spring容器" p:address-ref="Address" p:car-ref="car"></bean>

<!--可以使用autowire属性指定自动装配的形式,byName根据bean的名字和当前bean的setter风格的属性名进行自动装配,有匹配的则装配进来,否则不装配 -->

<bean id="person1" class="com.study.autowire.Person"

p:name="Spring容器" autowire="byName"></bean>

<!--可以使用autowire属性指定自动装配的形式,byType根据bean的名字和当前bean的类型进行自动装配,有匹配的则装配进来,否则不装配 -->

<!-- <bean id="person2" class="com.study.autowire.Person"

p:name="Spring容器" autowire="byType"></bean> -->

<bean id="address1" class="com.study.autowire.Address"

p:city="北京" p:street="万里长城">

</bean>

<!-- bean的继承,Xml文件是树形存储结构,按父节点,子节点存储的,通过parent属性指定继承哪个bean -->

<bean id="address2" p:city="广州" p:street="颐和园" parent="address1">

</bean>

<!--抽象Bean,不能被实例化,只能作为其它的模板bean,指定abstract属性=true,IOC容器将不会对其实例化 -->

<bean id="address3" class="com.study.autowire.Address" abstract="true"

p:city="北京" p:street="万里长城">

</bean>

<!-- bean的继承,Xml文件是树形存储结构,按父节点,子节点存储的,通过parent属性指定继承哪个bean -->

<bean id="address4" p:city="广州" p:street="颐和园" parent="address3">

</bean>

<bean id="car1" class="com.study.autowire.Car"

p:brand="依赖car" p:price="330000"></bean>

<!-- bean前置依赖,只有被依赖的对象被IOC容器实例化之后 -->

<bean id="person2" class="com.study.autowire.Person"

p:name="依赖注入" p:address-ref="address2" depends-on="car1"></bean>

</beans>

2: bean

package com.study.autowire;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAutoWireMain {

public static void main(String[] args) {

ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-relation.xml");

Person person1=(Person) ctx.getBean("person1");

System.out.println(person1);

// Person person2=(Person) ctx.getBean("person2");

// System.out.println(person2);

Address address1=(Address) ctx.getBean("address1");

Address address2=(Address) ctx.getBean("address2");

System.out.println(address1);

System.out.println(address2);

Address address4=(Address) ctx.getBean("address4");

System.out.println(address4);

Person person2=(Person) ctx.getBean("person2");

System.out.println(person2);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: