Spring黑马:依赖注入(Dependency Injection)


1.IOC的作用:降低程序之间的耦合(依赖关系)
2.依赖关系的管理:以后都交给spring来维护。
在当前类需要用到其他类的对象,由spring来提供,我们只需要在配置文件中说明依赖关系的维护,称为依赖注入。
3.依赖注入:
能注入的数据有3类:基本类型和Spring;其他bean类型(在配置文件中或者注解配置过的bean); 复杂类型/集合类型
注入的方式有3种:(1)使用构造函数提供;(2)使用set方法提供;(3)使用注解提供


1.依赖注入的3种方式(获取bean对象)

1.1 构造函数注入

使用的标签:constructor-arg
标签出现的位置:bean标签的内部
标签中的属性:

  • type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型。
  • index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置从0开始
  • name:用于指定给构造函数中指定名称的参数赋值 (常用)
    以上三个用于指定给构造函数中哪个参数赋值
  • value:用于提供基本类型和String类型的数据
  • ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象

优势:在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。
弊端:改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。
在这里插入图片描述

 <bean id="accountService" class="com.jh.service.impl.AccountServiceImpl">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="23"></constructor-arg>
        <constructor-arg name="birthday" ref="date"></constructor-arg>
 </bean>
    
    <!--配置一个日期对象-->
    <bean id="date" class="java.util.Date"></bean>      

1.2 使用set方法注入

涉及的标签:property,出现的位置在 bean标签的内部
标签属性:

  • name: 用于指定注入时所调用的set方法名称
  • value:用于提供基本类型和String类型的数据
  • ref :用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象

优势:创建对象没有明确的限制,可以直接使用默认构造函数
弊端:如果有某个成员必须有值,则获取对象是有可能set方法没有执行。
在这里插入图片描述

 <bean id="accountService2" class="com.jh.service.impl.AccountServiceImpl2">
        <property name="name" value="李四"></property>
        <property name="age" value="21"></property>
        <property name="birthday" ref="date"></property>
    </bean>
   
    <!--配置一个日期对象-->
    <bean id="date" class="java.util.Date"></bean> 

1.3 注解注入

用于创建bean对象的注解


2. 依赖注入的3类数据(其实就是设定参数值)

在依赖注入的3种方式中都有数据的注入
(1) 基本类型和Spring的注入
在这里插入图片描述
(2)其他bean类型(在配置文件中或者注解配置过的bean)的注入
在这里插入图片描述
(3) 复杂类型的注入/集合类型的注入
用于给List结构集合注入的标签: list,array,set
用于给Map结构集合注入的标签: map props
结构相同,标签可以互换
在这里插入图片描述

<bean id="accountService3" class="com.jh.service.impl.AccountServiceImpl3">
        <property name="myStrs">
            <array>
                <value>胡歌</value>
                <value>胡天</value>
                <value>沙溢</value>
            </array>
        </property>
        <property name="myList">
            <list>
                <value>小狗</value>
                <value>小猫</value>
                <value>老鼠</value>
            </list>
        </property>
        <property name="mySet">
            <set>
                <value>玫瑰</value>
                <value>菊花</value>
                <value>康乃馨</value>
            </set>
        </property>
        <property name="myMap">
            <map>
                <entry key="a" value="苹果"></entry>
                <entry key="b">
                    <value>香蕉</value>
                </entry>
            </map>
        </property>
        <property name="myProps">
            <props>
                <prop key="1">ccc</prop>
                <prop key="2">dd</prop>
            </props>
        </property>
    </bean>

3. 案例

bean.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">
    <!--(1)构造函数注入:-->
    <bean id="accountService" class="com.jh.service.impl.AccountServiceImpl">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="23"></constructor-arg>
        <constructor-arg name="birthday" ref="date"></constructor-arg>
    </bean>
    <!--配置一个日期对象-->
    <bean id="date" class="java.util.Date"></bean>

    <!--(2)使用set方法注入:-->
    <bean id="accountService2" class="com.jh.service.impl.AccountServiceImpl2">
        <property name="name" value="李四"></property>
        <property name="age" value="21"></property>
        <property name="birthday" ref="date"></property>
    </bean>

    <!--复杂类型的注入/集合类型的注入-->
    <bean id="accountService3" class="com.jh.service.impl.AccountServiceImpl3">
        <property name="myStrs">
            <array>
                <value>胡歌</value>
                <value>胡天</value>
                <value>沙溢</value>
            </array>
        </property>
        <property name="myList">
            <list>
                <value>小狗</value>
                <value>小猫</value>
                <value>老鼠</value>
            </list>
        </property>
        <property name="mySet">
            <set>
                <value>玫瑰</value>
                <value>菊花</value>
                <value>康乃馨</value>
            </set>
        </property>
        <property name="myMap">
            <map>
                <entry key="a" value="苹果"></entry>
                <entry key="b">
                    <value>香蕉</value>
                </entry>
            </map>
        </property>
        <property name="myProps">
            <props>
                <prop key="1">ccc</prop>
                <prop key="2">dd</prop>
            </props>
        </property>
    </bean>
</beans>

com\jh\service业务层接口

package com.jh.service;
public interface IAccountService {
    void saveAccount();
}

com\jh\service\impl业务层3个实现类

(1)构造函数注入:AccountServiceImpl

package com.jh.service.impl;
import com.jh.service.IAccountService;
import java.util.Date;
public class AccountServiceImpl implements IAccountService {
    //如果是经常变化的数据,并不适用于注入的方式
    private String name;
    private Integer age;
    private Date birthday;
    public AccountServiceImpl(String name, Integer age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }
    public void  saveAccount(){
        System.out.println("AccountServiceImpl执行了"+name+","+age+","+birthday);
    }
}

(2)使用set方法注入:AccountServiceImpl2

package com.jh.service.impl;
import com.jh.service.IAccountService;
import java.util.Date;
public class AccountServiceImpl2 implements IAccountService {
    //如果是经常变化的数据,并不适用于注入的方式
    private String name;
    private Integer age;
    private Date birthday;

    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 Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public void  saveAccount(){
        System.out.println("AccountServiceImpl2执行了"+name+","+age+","+birthday);
    }
}

注入复杂类型/集合类型 数据:AccountServiceImpl3

package com.jh.service.impl;
import com.jh.service.IAccountService;
import java.util.*;
public class AccountServiceImpl3 implements IAccountService {
    private String[] myStrs;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String,String> myMap;
    private Properties myProps;

    public String[] getMyStrs() {
        return myStrs;
    }

    public void setMyStrs(String[] myStrs) {
        this.myStrs = myStrs;
    }

    public List<String> getMyList() {
        return myList;
    }

    public void setMyList(List<String> myList) {
        this.myList = myList;
    }

    public Set<String> getMySet() {
        return mySet;
    }

    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }

    public Map<String, String> getMyMap() {
        return myMap;
    }

    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }

    public Properties getMyProps() {
        return myProps;
    }

    public void setMyProps(Properties myProps) {
        this.myProps = myProps;
    }

    public void  saveAccount(){
        //数组需要用 Arrays
        System.out.println(Arrays.toString(myStrs));
        System.out.println(myList);
        System.out.println(mySet);
        System.out.println(myMap);
        System.out.println(myProps);
    }
}

Client.java

package com.jh.ui;
import com.jh.service.IAccountService;
import com.jh.service.impl.AccountServiceImpl;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Client {

    public static void main(String[] args) {
        //--------ApplicationContext 立即加载----------------------
        //1.获取核心容器对象
        ApplicationContext ac= new ClassPathXmlApplicationContext("bean.xml");
        //2.根据id获取Bean对象
        //(1)构造函数注入
        IAccountService as= (IAccountService)ac.getBean("accountService");
        as.saveAccount();//AccountServiceImpl执行了张三,23,Sat Feb 15 17:13:06 CST 2020

        //(2)使用set方法注入
        IAccountService as2= (IAccountService)ac.getBean("accountService2");
        as2.saveAccount();//AccountServiceImpl2执行了李四,21,Sat Feb 15 17:44:31 CST 2020

        //使用set方法注入 复杂类型/集合类型 数据
        IAccountService as3= (IAccountService)ac.getBean("accountService3");
        as3.saveAccount();//
        }
    }
发布了88 篇原创文章 · 获赞 14 · 访问量 4902

猜你喜欢

转载自blog.csdn.net/JH39456194/article/details/104345262