Spring(四)Spring之自动装配(autowire)

什么是自动装配?

spring IOC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自
动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。由于
autowire可以针对单个bean进行设置,因此可以让有些bean使用autowire,有些bean不采用。autowire的
方便之处在减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥


在配置文件中给域属性注入值时除了使用
<property name="car" ref="car"></property>

(外面的bean节点我没有写,在前面已经有写过,在这里简单提一下)

还有一种方法就是域属性自动装配设置bean节点中的autowire属性来给他注入值

下面用个例子来说一下

首先是准备工作

先创建两个类Student和Car,类中的内容如下

package demo06;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by mycom on 2018/3/5.
 */
public class Student {
    private String name;
    private Integer age;
    private Car car;

    public Student() {
    }

    public Student(String name, Integer age) {

        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public Car getCar() {
        return car;
    }

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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by mycom on 2018/3/5.
 */
public class Car {
    private String color;
    private String brand;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {

        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

在配置文件中进行配置

<?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:aop="http://www.springframework.org/schema/aop"
       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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

        <bean id="car" class="demo06.Car">
            <property name="color" value="红色"></property>
            <property name="brand" value="布加迪"></property>
        </bean>
        <bean id="littleCar" class="demo06.LittleCar">
            <property name="color" value="红色"></property>
            <property name="brand" value="布加迪"></property>
        </bean>
        <bean id="student" class="demo06.Student" autowire="byName">
            <property name="name" value="小明"></property>
            <property name="age" value="12"></property>
        </bean>
</beans>

在这里说明一下:

autowire的值有两个 byName和byType 

byName就是根据Spring管理的Car的id值来进行属性注入

byType 就是根据Spring管理的Car的类型注入,所以这种注入方式有两种

原文地址:https://www.cnblogs.com/my-123/p/8521735.html