spring IOC用ref属性实现bean之间的引用

如下图所示,用ref属性建立bean之间的引用

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

下面是car2的bean

<bean id="car2" class="com.beans.Car">
    <constructor-arg value="Boma"></constructor-arg>
    <constructor-arg value="ChangSha"></constructor-arg>
    <constructor-arg value="240" type="int"></constructor-arg>
    </bean>

下面贴出整个项目的完整代码,可以自己新建一个项目贴上去试下。
运行主类

package com.beans;

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

public class Main {

	public static void main(String[] args) {
		
       //1.创建spring的IOC容器对象
		//ApplicationContext 代表IOC容器 是 BeanFactory 接口的子接口
		
		// ClassPathXmlApplicationContext: 是 ApplicationContext的实现类,从类路径下来加载配置文件
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //2.从IOC容器中获取bean实例
        //利用id定位到IOC容器中的bean
      
        Car car=(Car)ctx.getBean("car");
        System.out.println(car);
        
        Car car2=(Car)ctx.getBean("car2");
        System.out.println(car2);
        
        Person person=(Person)ctx.getBean("person");
        System.out.println(person);
	}

}

car类

package com.beans;

public class Car {
       
	   private String brand;
	   private String corp;
       private double price;
       private int maxSpeed;
       
       public Car(String brand, String corp, double price) {
   		super();
   		this.brand = brand;
   		this.corp = corp;
   		this.price = price;
   	}
       
       public Car(String brand, String corp, int maxSpeed) {
   		super();
   		this.brand = brand;
   		this.corp = corp;
   		this.maxSpeed = maxSpeed;
   	}

       @Override
   	public String toString() {
   		return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
   	}

      
}

Person类

package com.beans;

public class Person {
   
	private String name;
	private int age;
    private Car car;
    @Override
   	public String toString() {
   		return "Person [name=" + name + ", age=" + age + ", 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;
 	}
}

applicationContext. 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 
    class :bean 的全类名,通过反射的方式在IOC容器中创建bean,所以要求必须要有无参的构造器
    id: 表示容器中唯一的id
    -->
    
    
    
    <!--通过构造方法配置bean的属性 -->
    <bean id="car" class="com.beans.Car">
    <constructor-arg value="Audi" index="0"></constructor-arg>
    <constructor-arg  index="1">
    <value><![CDATA[<ShangHai>]]></value>
    </constructor-arg>
    <constructor-arg value="30000" index="2"></constructor-arg>
    </bean>
    
    <bean id="car2" class="com.beans.Car">
    <constructor-arg value="Boma"></constructor-arg>
    <constructor-arg value="ChangSha"></constructor-arg>
    <constructor-arg value="240" type="int"></constructor-arg>
    </bean>
    
    <bean id="person" class="com.beans.Person">
    <property name="name" value="tom"></property>
    <property name="age" value="20"></property>
    <!-- 可以使用property的ref属性建立bean之间的引用关系-->
    <property name="car" ref="car2"></property>
    </bean>
    
    
</beans>

运行结果如下
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_37774171/article/details/85246185
今日推荐