zoukankan      html  css  js  c++  java
  • 04 Spring的依赖注入

    依赖注入:Dependency Injection。它是 spring 框架核心 ioc 的具体实现。

    我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。

    ioc 解耦只是降低他们的依赖关系,但不会消除。例如:我们的业务层仍会调用持久层的方法。

    那这种业务层和持久层的依赖关系,在使用 spring 之后,就让 spring 来维护了。

    简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。

    构造函数注入

    顾名思义,就是使用类中的构造函数,给成员变量赋值。注意,赋值的操作不是我们自己做的,而是通过配置的方式,让 spring 框架来为我们注入。具体代码如下:

    Car.java

    package com.itzn.model;
    import java.util.Date;
    public class Car {
        private String name;
        private long price;
        private Date buyDate;
    
        public Car(String _name, long _price, Date _buyDate) {
            name = _name;
            price = _price;
            buyDate = _buyDate;
        }
    
        public void MyStr() {
            System.out.println("name=" + name + ",price=" + price + ",buyDate=" + buyDate);
        }
    }

    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">
        <!--构造函数注入-->
        <bean id="car" class="com.itzn.model.Car">
            <constructor-arg name="_name" value="逍客"></constructor-arg>
            <constructor-arg name="_price" value="200000"></constructor-arg>
            <constructor-arg name="_buyDate" ref="now"></constructor-arg>
        </bean>
        <bean id="now" class="java.util.Date"></bean>
    </beans>

    SpringTest.java

    public class SpringTest {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
            Car car=(Car) ac.getBean("car");
            car.MyStr();
    
        }
    }

    使用构造函数的方式,要求:

    类中需要提供一个对应参数列表的构造函数。

    涉及的标签:

    constructor-arg

    属性:

    index:指定参数在构造函数参数列表的索引位置

    type:指定参数在构造函数中的数据类型

    name:指定参数在构造函数中的名称 用这个找给谁赋值

    =======上面三个都是找给谁赋值,下面两个指的是赋什么值的==============

    value:它能赋的值是基本数据类型和 String 类型

    ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean

    优势:在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。

    弊端:改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。

    set 方法注入

    顾名思义,就是在类中提供需要注入成员的 set 方法。具体代码如下:

    Car2.java

    package com.itzn.model;
    import java.util.Date;
    public class Car2 {
        private String name;
        private long price;
        private Date buyDate;
    
        public void setName(String name) {
            this.name = name;
        }
        public void setPrice(long price) {
            this.price = price;
        }
        public void setBuyDate(Date buyDate) {
            this.buyDate = buyDate;
        }
        public void MyStr() {
            System.out.println("name=" + name + ",price=" + price + ",buyDate=" + buyDate);
        }
    }

    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">
        <!--构造函数注入-->
        <bean id="car" class="com.itzn.model.Car">
            <constructor-arg name="_name" value="逍客"></constructor-arg>
            <constructor-arg name="_price" value="200000"></constructor-arg>
            <constructor-arg name="_buyDate" ref="now"></constructor-arg>
        </bean>
        <bean id="now" class="java.util.Date"></bean>
        <!--set注入-->
    <bean id="dasauto" class="com.itzn.model.Car2">
        <property name="name" value="朗逸"></property>
        <property name="price" value="150000"></property>
        <property name="buyDate" ref="now"></property>
    </bean>
    </beans>

    SpringTest.java

    package com.itzn.test;
    
    import com.itzn.model.Car;
    import com.itzn.model.Car2;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringTest {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
            Car car=(Car) ac.getBean("car");
            Car2 car2=(Car2) ac.getBean("dasauto");
            car.MyStr();
            car2.MyStr();
        }
    }

    使用 set 方法的方式

    涉及的标签:

    property

    属性:

    name:找的是类中 set 方法后面的部分

    ref:给属性赋值是其他 bean 类型的

    value:给属性赋值是基本数据类型和 string 类型的

    实际开发中,此种方式用的较多。

    优势:创建对象时没有明确的限制,可以直接使用默认构造函数

    弊端:如果有某个成员必须有值,则获取对象是有可能set方法没有执行。

    集合属性注入

    顾名思义,就是给类中的集合成员传值,它用的也是set方法注入的方式,只不过变量的数据类型都是集合。

    我们这里介绍注入数组,List,Set,Map,Properties。具体代码如下:

    Car3.java

    package com.itzn.model;
    import java.util.*;
    public class Car3 {
        private String[] myStrs;
        private List<String> myList;
        private Set<String> mySet;
        private Map<String,String> myMap;
        private Properties myProps;
        public void setMyStrs(String[] myStrs) {
            this.myStrs = myStrs; }
        public void setMyList(List<String> myList) {
            this.myList = myList; }
        public void setMySet(Set<String> mySet) {
            this.mySet = mySet; }
        public void setMyMap(Map<String, String> myMap) {
            this.myMap = myMap; }
        public void setMyProps(Properties myProps) {
            this.myProps = myProps; }
    
        public void MyStr() {
            System.out.println(Arrays.toString(myStrs));
            System.out.println(myList);
            System.out.println(mySet);
            System.out.println(myMap);
            System.out.println(myProps);
        }
    }

    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">
        <!--构造函数注入-->
        <bean id="car" class="com.itzn.model.Car">
            <constructor-arg name="_name" value="逍客"></constructor-arg>
            <constructor-arg name="_price" value="200000"></constructor-arg>
            <constructor-arg name="_buyDate" ref="now"></constructor-arg>
        </bean>
        <bean id="now" class="java.util.Date"></bean>
        <!--set注入-->
        <bean id="dasauto" class="com.itzn.model.Car2">
            <property name="name" value="朗逸"></property>
            <property name="price" value="150000"></property>
            <property name="buyDate" ref="now"></property>
        </bean>
    
        <!--集合属性注入-->
        <bean id="autos" class="com.itzn.model.Car3">
            <!-- 在注入集合数据时,只要结构相同,标签可以互换 -->
            <!-- 给数组注入数据 -->
            <property name="myStrs">
                <set>
                    <value>一汽大众</value>
                    <value>上海大众</value>
                    <value>一汽奥迪</value>
                </set>
            </property>
            <!-- 注入 list 集合数据 -->
            <property name="myList">
                <array>
                    <value>奔驰</value>
                    <value>宝马</value>
                    <value>奥迪</value>
                </array>
            </property>
            <!-- 注入 set 集合数据 -->
            <property name="mySet">
                <list>
                    <value>丰田</value>
                    <value>本田</value>
                    <value>日产</value>
                </list>
            </property>
            <!-- 注入 Map 数据 -->
            <property name="myMap">
                <props>
                    <prop key="dasauto">大众迈腾</prop>
                    <prop key="aodi">A6L</prop>
                </props>
            </property>
            <!-- 注入 properties 数据 -->
            <property name="myProps">
                <map>
                    <entry key="keyA" value="aaa">
    
                    </entry>
                    <entry key="keyB">
                        <value>bbb</value>
                    </entry>
                </map>
            </property>
        </bean>
    </beans>

    SpringTest.java

    package com.itzn.test;
    import com.itzn.model.Car;
    import com.itzn.model.Car2;
    import com.itzn.model.Car3;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringTest {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
            Car car=(Car) ac.getBean("car");
            Car2 car2=(Car2) ac.getBean("dasauto");
            Car3 car3=(Car3) ac.getBean("autos");
            car.MyStr();
            car2.MyStr();
            car3.MyStr();
        }
    }

    复杂类型的注入/集合类型的注入

    用于给List结构集合注入标签:

    list array set

    用于Map结构集合注入的标签:

    map props

    结构相同,标签可以互换

    你喜欢的代码:点击下载

  • 相关阅读:
    android基于adb的性能测试
    mysql-3 数据表的创建、增删改查
    17.3.16---python内建函数
    17.3.15---C语言详解FILE文件操作
    17.3.15---关于GPIO控制流水灯的信息
    17.3.15---关于GPIO学习笔记
    17.3.14--对于时钟的知识,涉及到的总结
    17.3.14对于面向对象的理解
    17.3.13--pycharm2和3的常识问题
    17.3.13--python编码问题
  • 原文地址:https://www.cnblogs.com/itmu89/p/12080620.html
Copyright © 2011-2022 走看看