Spring依赖注入的简介说明
Spring依赖注入的概念说明
Spring依赖注入DI(Dependency Injection)同IoC(控制反转)都是操作的同一件事情它们两者从不同的角度描述同一概念,在Spring中使用依赖注入可轻松的管理和测试应用程序
在以前的Java实例中,当需使用另一个Java实例时,
我们需使用new关键字获取一个调用者的实例,但是在Spring管理中,调用者无需创建,这些重复的工作都由Spring容器创建,我们将这种操作称之为"控制反转"
当Spring容器创建实例后,可自动会创建的实例注入指定的属性值我们将这种操作称之为"依赖注入"
在Spring中依赖注入有以下两种方式:
方式一: setter 注入IoC 容器使用 setter 方法注入被依赖的实例。通过调用无参构造器或无参 static 工厂方法实例化 Bean 后,调用该 Bean 的 setter 方法,即可实现基于 setter 的DI
方式二:构造函数注入
IoC 容器使用构造函数注入被依赖的实例。可以通过调用带参数的构造函数实现依赖注入,每个参数代表一个依赖
在Spring中依赖注入的流程
调用默认的构造方法实例化--->使用Java反射机制调用set方法对属性注入为了Spring Bean能够更好的操作Bean
1.我们需提供一个无参数构造方法
2.我们需为注入的属性添加一个set方法
使用setter注入时
在Spring bean.xml配置文件中,可使用 <bean> 元素的子元素 <property> 为每个属性注入值
使用构造注入时,在配置文件中,主要使用 <constructor-arg> 标签定义构造方法的参数,
使用其 value 属性(或子元素)设置该参数的值。
在 <constructor-arg>标签中,包含 ref、value、type、index 等属性
value 属性用于注入基本数据类型以及字符串类型的值
ref 属性用于注入已经定义好的 Bean
type 属性用来指定对应的构造函数,当构造函数有多个参数时,可以使用 index 属性指定参数的位置,index 属性值从 0 开始
例1
- 在Eclispe中创建 SpringDemo 项目
 - 在src目录下创建 com.java265 包
 - 添加相应的 jar 包,可以参考我的第一个Spring程序
 - 在 com.java265 包下创建 Person、Man 和 MainApp 类
 - 在 src 目录下创建 Spring 配置文件 Beans.xml
 - 运行 SpringDemo 项目
 
package com.java265;
public class Person {
    private Man man;
    public Person(Man man) {
        System.out.println("在Person的构造函数内");
        this.man = man;
    }
    public void man() {
        man.show();
    }
}
 Man 类
package com.java265;
public class Man {
    private String name;
    private int age;
    public Man() {
        System.out.println("在man的构造函数内");
    }
    public Man(String name, int age) {
        System.out.println("在man的有参构造函数内");
        this.name = name;
        this.age = age;
    }
    public void show() {
        System.out.println("名称:" + name + "\n年龄:" + age);
    }
    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;
    }
}
 Beans.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-3.0.xsd"> <bean class="com.java265.Man" id="man"> <constructor-arg value="java265.com"> <constructor-arg type="int" value="888"> </constructor-arg></constructor-arg></bean> <bean class="com.java265.Person" id="person"> <constructor-arg ref="man" type="java.lang.String"> </constructor-arg></bean> </beans>MainApp 类
package com.java265;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        Person person = (Person) context.getBean("person");
        person.man();
    }
}
 运行结果-----在man的有参构造函数内
在Person的构造函数内
名称:java265.com
年龄:888
setter注入
下面使用 <property> 标签实现 setter 注入
在 <property> 标签中,
包含 name、ref、value 等属性
name 用于指定参数名称
value 属性用于注入基本数据类型以及字符串类型的值
ref 属性用于注入已经定义好的 Bean
例2
可基于例1修改Man 类的内容
package com.java265;
public class Man {
    private String name;
    private int age;
    public Man() {
        System.out.println("在man的构造函数内");
    }
    public Man(String name, int age) {
        System.out.println("在man的有参构造函数内");
        this.name = name;
        this.age = age;
    }
    public void show() {
        System.out.println("名称:" + name + "\n年龄:" + age);
    }
    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;
    }
}
 Beans.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-3.0.xsd"> <bean class="com.java265.Person" id="person"> <property name="man" ref="man"> </property></bean> <bean class="com.java265.Man" id="man"> <property name="name" value="java265.com"> <property name="age" value="888"> </property></property></bean> </beans>运行结果-----
在man的构造函数内
在setMan方法内
名称:java265.com
年龄:888
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

			
               
               
               
               
          
