Spring全回顾bean之间的引用关系

首先创建一个类
注意:此类的Car属性实际上引用了一个Carr对象,可以创建一个Car,见上篇文章:Spring全回顾之构造方法注入值

package com.kk.spring.beans;

public class Person {

    private String name;
    private int age;
    private 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
                + ", getName()=" + getName() + ", getAge()=" + getAge()
                + ", getCar()=" + getCar() + ", getClass()=" + getClass()
                + ", hashCode()=" + hashCode() + ", toString()="
                + super.toString() + "]";
    }

    public Person() {}

    public Person(String name, int age, Car car) {
        super();
        this.name = name;
        this.age = age;
        this.car = car;
    }


}

在配置文件,配置一个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: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.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    <bean id="person" class="com.kk.spring.beans.Person">
        <property name="name" value="Tom"></property>
        <property name="age" value="22"></property>
        <!-- 可以使用property的ref属性建立bean之间的引用关系,引用外部bean  -->
        <property name="car" ref="car2"></property> 
    </bean>
</beans>

上面方式以引用一个外部bean

当然也可以创建一个内部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: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.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    <bean id="person" class="com.kk.spring.beans.Person">
        <property name="name" value="Tom"></property>
        <property name="age" value="22"></property>
        <!-- 可以使用property的ref属性建立bean之间的引用关系,引用外部bean  -->
        <property name="car" ref="car2"></property> 

        <!-- 内部Bean,它不能被外部引用,只能在内部使用 -->
        <property name="car">
            <bean class="com.kk.spring.beans.Car">
                <constructor-arg value="Ford"></constructor-arg>
                <constructor-arg value="ChangAn"></constructor-arg>
                <constructor-arg value="200000" type=""></constructor-arg>
            </bean>
        </property>
        <property name="car.maxSpeed" value="270"></property>
    </bean>

</beans>

写一个测试类:

package com.kk.spring.beans;

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

public class test {
public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicaContext.xml");
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}
}

猜你喜欢

转载自blog.csdn.net/fx9590/article/details/80375195