引用其它bean

组成应用程序的Bean经常需要相互协作以完成应用程序的功能,要使Bean能够相互访问,就必须在Bean配置文件中指定对Bean的引用。在Bean的配置文件中,可以通过<ref>元素或ref属性为Bean的属性或构造器参数指定对Bean的引用。也可以在属性或构造器里包含Bean的声明,这样的Bean称为内部Bean.

首先:简单地举一个例子:代码如下:

使用<ref>元素对bean的引用的Spring.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
   <!-- <bean id="helloWord" class="HelloWord">
        <property name="name" value="小米"></property>
    </bean>-->
    <bean id="car" class="Car">
        <constructor-arg value="baoma" index="1"></constructor-arg>
        <constructor-arg value="12" index="0"></constructor-arg>
        <constructor-arg value="10000" index="2" type="double"></constructor-arg>
    </bean>
    <bean id="person" class="Person">
        <property name="name" value="for"></property>
        <property name="age" value="56"></property>
        <!--引用bean时,也可以使用元素ref去引用-->
        <property name="car">
            <ref bean="car"/>
        </property>
    </bean>
</bean>

使用 ref属性对bean的引用的Spring.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
   <!-- <bean id="helloWord" class="HelloWord">
        <property name="name" value="小米"></property>
    </bean>-->
    <bean id="car" class="Car">
        <constructor-arg value="baoma" index="1"></constructor-arg>
        <constructor-arg value="12" index="0"></constructor-arg>
        <constructor-arg value="10000" index="2" type="double"></constructor-arg>
    </bean>
    <bean id="person" class="Person">
        <property name="name" value="for"></property>
        <property name="age" value="56"></property>
        <!--引用bean时,使用属性ref去引用-->
        <property name="car" ref="car"></property>
    </bean>
   <!-- <bean id="helloWord1" class="HelloWord">
        <property name="name" value="小车"></property>
    </bean>-->
</beans>

使用内部Bean对Spring.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="person" class="Person">
        <property name="name" value="for"></property>
        <property name="age" value="56"></property>
        <property name="car">
            <bean id="car" class="Car">
                <constructor-arg value="baoma" index="1"></constructor-arg>
                <constructor-arg value="12" index="0"></constructor-arg>
                <constructor-arg value="10000" index="2" type="double"></constructor-arg>
            </bean>
        </property>
    </bean>
</beans>

 

 person类文件:

public class Person {
    public String name;
    public int age;
    public Car car;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", car=" + car +
                '}';
    }
}

car类的文件:

 



import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CarMain {
    public static void main(String [] mains){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("spring1.xml");
        Person person = (Person) applicationContext.getBean ("person");
        System.out.println (person);



    }
}
public class Car {
    public String name;
    public int sum;
    public int num;
    public double price;

    public Car(int sum, String name, double price) {
        this.name = name;
        this.sum = sum;
        this.price = price;
    }
    public Car(int sum,String name,int num){
        this.sum = sum;
        this.name = name;
        this.num = num;
    }
    public Car(String name,int sum,int num){
        this.name = name;
        this.sum = sum;
        this.num = num;
    }
    public String toString(){
        return "车名:" + name + "数量:" + sum + "单价:" + price + "余数:"+ num;
    }
}

 

 main方法:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CarMain {
    public static void main(String [] mains){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("spring1.xml");
        Person person = (Person) applicationContext.getBean ("person");
        System.out.println (person);



    }
}

 输出结果:

 注意点:内部Bean不能被外部Bean去引用的,只能在内部使用。

 以上的引用功能,如果用构造注入去配置属性时,原理

public class Person {
    public String name;
    public int age;
    public Car car;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }
    public Person(String name,int age,Car car){
        this.name = name;
        this.age = age;
        this.car = car;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", car=" + car +
                '}';
    }
}

也是一样的。

Spring.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--用构造器去配置属性-->
    <bean id="person" class="Person">
        <constructor-arg value="for"></constructor-arg>
        <constructor-arg value="56"></constructor-arg>
        <constructor-arg>
            <bean id="car" class="Car">
                <constructor-arg value="baoma" index="1"></constructor-arg>
                <constructor-arg value="12" index="0"></constructor-arg>
                <constructor-arg value="10000" index="2" type="double"></constructor-arg>
            </bean>
        </constructor-arg>
    </bean>
</beans>

 

Person类的配置:

public class Person {
    public String name;
    public int age;
    public Car car;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }
    public Person(String name,int age,Car car){
        this.name = name;
        this.age = age;
        this.car = car;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", car=" + car +
                '}';
    }
}

 

 car类文件:

public class Car {
    public String name;
    public int sum;
    public int num;
    public double price;

    public Car(int sum, String name, double price) {
        this.name = name;
        this.sum = sum;
        this.price = price;
    }
    public Car(int sum,String name,int num){
        this.sum = sum;
        this.name = name;
        this.num = num;
    }
    public Car(String name,int sum,int num){
        this.name = name;
        this.sum = sum;
        this.num = num;
    }
    public String toString(){
        return "车名:" + name + "数量:" + sum + "单价:" + price + "余数:"+ num;
    }
}

 

 main文件

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CarMain {
    public static void main(String [] mains){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("spring1.xml");
        Person person = (Person) applicationContext.getBean ("person");
        System.out.println (person);



    }
}

 

 输出结果:

其实原理都是一样的,只是注入的方式不同而已。 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值