zoukankan      html  css  js  c++  java
  • 【Spring入门系列】IOC控制反转、DI依赖注入

    什么是IOC控制反转

    Inversion of Control,即控制反转,它不是什么技术,而是一种设计思想。传统的创建对象的方法是直接通过 new 关键字,而 spring 则是通过 IOC 容器来创建对象,也就是说我们将创建对象的控制权交给了 IOC 容器。IOC 让程序员不再关注怎么去创建对象,而是关注与对象创建之后的操作,把对象的创建、初始化、销毁等工作交给spring容器来做。

    Spring创建对象的三种方式

    第一种方法:利用默认的构造方法

    <?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">
    
    
        <!-- 创建对象的第一种方式:利用无参构造器
            id:唯一标识符
            class:类的全类名  -->
        <bean id="helloIoc" class="com.ysdrzp.ioc.HelloIoc" ></bean>
        <!-- 别名属性name和bean的id属性对应 -->
        <alias name="helloIoc" alias="helloIoc2"/>
    
    </beans>
    public class HelloIoc {
    
        public HelloIoc(){
            System.out.println("HelloIoc对象创建完成");
        }
        
        public void sayHello(){
            System.out.println("Hello Ioc");
        }
    }
    public class TestHelloIoc {
    
        @Test
        public void testCreateObjectByConstrutor(){
            //1、启动 spring 容器
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            //2、从 spring 容器中取出数据
            HelloIoc IOC = (HelloIoc) context.getBean("helloIoc");
            //3、通过对象调用方法
            IOC.sayHello();
            //利用配置文件 alias 别名属性创建对象
            HelloIoc IOC2 = (HelloIoc) context.getBean("helloIoc2");
            IOC2.sayHello();
        }
    
    }

    第二种方式:利用静态工厂方法

    <?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">
    
    
        <!-- 创建对象的第一种方式:利用无参构造器
            id:唯一标识符
            class:类的全类名  -->
        <bean id="helloIoc" class="com.ysdrzp.ioc.HelloIoc" ></bean>
        <!-- 别名属性name和bean的id属性对应 -->
        <alias name="helloIoc" alias="helloIoc2"/>
    
        <!-- 创建对象的第二种方式:利用静态工厂方法
            factory-method:静态工厂类的获取对象的静态方法
            class:静态工厂类的全类名  -->
        <bean id="helloStaticFactory" factory-method="getInstances" class="com.ysdrzp.ioc.HelloStaticFactory"></bean>
    
    </beans>
    public class HelloStaticFactory {
    
        public HelloStaticFactory(){
            System.out.println("HelloStaticFactory constructor");
        }
    
        //静态工厂方法
        public static HelloIoc getInstances(){
            return new HelloIoc();
        }
    }
    public class TestHelloIoc {
    
        /**
         * Spring 容器利用构造函数创建对象
         */
        @Test
        public void testCreateObjectByConstrutor(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            HelloIoc IOC = (HelloIoc) context.getBean("helloIoc");
            IOC.sayHello();
            //利用配置文件 alias 别名属性创建对象
            HelloIoc IOC2 = (HelloIoc) context.getBean("helloIoc2");
            IOC2.sayHello();
        }
    
        /**
         * Spring 容器利用静态工厂方法创建对象
         */
        @Test
        public void createObjectStaticFactory(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            HelloIoc helloIoc = (HelloIoc) context.getBean("helloStaticFactory");
            helloIoc.sayHello();
        }
    
    }

    注意:spring容器只负责调用静态工厂方法,而这个静态工厂方法内部实现由程序员完成

    第三种方式:利用实例工厂方法

    <?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">
    
    
        <!-- 创建对象的第一种方式:利用无参构造器
            id:唯一标识符
            class:类的全类名  -->
        <!--<bean id="helloIoc" class="com.ysdrzp.ioc.HelloIoc" ></bean>-->
        <!-- 别名属性name和bean的id属性对应 -->
        <!--<alias name="helloIoc" alias="helloIoc2"/>-->
    
        <!-- 创建对象的第二种方式:利用静态工厂方法
            factory-method:静态工厂类的获取对象的静态方法
            class:静态工厂类的全类名  -->
        <!--<bean id="helloStaticFactory" factory-method="getInstances" class="com.ysdrzp.ioc.HelloStaticFactory"></bean>-->
    
        <!-- 创建对象的第三种方式:利用实例工厂方法
            factory-bean:指定当前Spring中包含工厂方法的beanID
            factory-method:工厂方法名称  -->
        <bean id="instanceFactory" class="com.ysdrzp.ioc.HelloInstanceFactory"></bean>
        <bean id="instance" factory-bean="instanceFactory" factory-method="getInstance"></bean>
    
    </beans>
    public class HelloInstanceFactory {
    
        public HelloInstanceFactory(){
            System.out.println("实例工厂方法构造函数");
        }
    
        //利用实例工厂方法创建对象
        public HelloIoc getInstance(){
            HelloIoc instanceIoc = new HelloIoc();
            return instanceIoc;
        }
    }
    public class TestHelloIoc {
    
        /**
         * Spring 容器利用构造函数创建对象
         */
        @Test
        public void testCreateObjectByConstrutor(){
            //1、启动 spring 容器
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            //2、从 spring 容器中取出数据
            HelloIoc IOC = (HelloIoc) context.getBean("helloIoc");
            //3、通过对象调用方法
            IOC.sayHello();
            //利用配置文件 alias 别名属性创建对象
            HelloIoc IOC2 = (HelloIoc) context.getBean("helloIoc2");
            IOC2.sayHello();
        }
    
        /**
         * Spring 容器利用静态工厂方法创建对象
         */
        @Test
        public void createObjectStaticFactory(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            HelloIoc helloIoc = (HelloIoc) context.getBean("helloStaticFactory");
            helloIoc.sayHello();
        }
    
        /**
         * Spring 容器利用实例工厂方法创建对象
         */
        @Test
        public void createObjectInstanceFactory(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            HelloIoc staticFactory = (HelloIoc) context.getBean("instance");
            staticFactory.sayHello();
        }
    
    }

    Spring容器创建对象的时机

    默认情况下,启动 Spring 容器便创建对象(遇到bean便创建对象);在spring的配置文件bean中有一个属性 lazy-init="default/true/false" ,如果lazy-init为"default/false"在启动spring容器时创建对象 ,如果lazy-init为"true",在真正用到对象时才去创建对象。

    两种方式对比:第一种方式可以在启动spring容器的时候,检查spring容器配置文件的正确性,如果再结合tomcat,如果spring容器不能正常启动,整个tomcat就不能正常启动。但是这样的缺点是把一些bean过早的放在了内存中,如果有数据,则对内存来是一个消耗,反过来,在第二种方式下,可以减少内存的消耗,但是不容易发现错误

    spring的bean中的scope:"singleton/prototype/request/session/global session" 

    scope="singleton",单例模式

    @Test
    public void test_scope_single_CreateObject(){
    ApplicationContext context
    = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloIoc hello1 = (HelloIoc) context.getBean("helloIoc"); HelloIoc hello2 = (HelloIoc) context.getBean("helloIoc"); System.out.println(hello1 == hello2); }
    输出结果:true

    scope=“prototype”, 多例模式

    @Test
    public void test_scope_prototype_CreateObject(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloIoc hello1 = (HelloIoc) context.getBean("helloIoc");
        HelloIoc hello2 = (HelloIoc) context.getBean("helloIoc");
        System.out.println(hello1 == hello2);
    }
    输出结果:false

    总结:在单例模式下,启动 spring 容器,便会创建对象;在多例模式下,启动容器并不会创建对象,获得 bean 的时候才会创建对象

    什么是DI依赖注入

    spring动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的。比如对象A需要操作数据库,以前我们总是要在A中自己编写代码来获得一个Connection对象,有了 spring我们就只需要告诉spring,A中需要一个Connection,至于这个Connection怎么构造,何时构造,A不需要知道。在系统运行时,spring会在适当的时候制造一个Connection,然后像打针一样,注射到A当中,这样就完成了对各个对象之间关系的控制。A需要依赖 Connection才能正常运行,而这个Connection是由spring注入到A中的,依赖注入的名字就这么来的。那么DI是如何实现的呢? Java 1.3之后一个重要特征是反射(reflection),它允许程序在运行的时候动态的生成对象、执行对象的方法、改变对象的属性,spring就是通过反射来实现注入的。简单来说什么是依赖注入,就是给属性赋值(包括基本数据类型和引用数据类型)。

    利用set方法给属性赋值

      <!-- property是用来描述一个类的属性
            基本类型的封装类、String等需要值的类型用value赋值
            引用类型用ref赋值 -->
        <bean id="person" class="com.ysdrzp.di.Person">
            <property name="pid" value="1"></property>
            <property name="pname" value="vae"></property>
            <property name="students">
                <ref bean="student"/>
            </property>
    
            <property name="lists">
                <list>
                    <value>1</value>
                    <ref bean="student"/>
                    <value>vae</value>
                </list>
            </property>
    
            <property name="sets">
                <set>
                    <value>1</value>
                    <ref bean="student"/>
                    <value>vae</value>
                </set>
            </property>
    
            <property name="maps">
                <map>
                    <entry key="m1" value="1"></entry>
                    <entry key="m2" >
                        <ref bean="student"/>
                    </entry>
                </map>
            </property>
    
            <property name="properties">
                <props>
                    <prop key="p1">p1</prop>
                    <prop key="p2">p2</prop>
                </props>
            </property>
    
        </bean>
        <bean id="student" class="com.ysdrzp.di.Student"></bean>
    
    public class Person {
    
        private Long pid;
        private String pname;
        private Student students;
        private List lists;
        private Set sets;
        private Map maps;
        private Properties properties;
    
        public Long getPid() {
            return pid;
        }
        public void setPid(Long pid) {
            this.pid = pid;
        }
        public String getPname() {
            return pname;
        }
        public void setPname(String pname) {
            this.pname = pname;
        }
        public Student getStudents() {
            return students;
        }
        public void setStudents(Student students) {
            this.students = students;
        }
        public List getLists() {
            return lists;
        }
        public void setLists(List lists) {
            this.lists = lists;
        }
        public Set getSets() {
            return sets;
        }
        public void setSets(Set sets) {
            this.sets = sets;
        }
        public Map getMaps() {
            return maps;
        }
        public void setMaps(Map maps) {
            this.maps = maps;
        }
        public Properties getProperties() {
            return properties;
        }
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    }
    public class TestDI {
    
        @Test
        public void testSet(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            Person person = (Person) context.getBean("person");
            System.out.println(person.getLists());
        }
    
    }

    利用构造函数给属性赋值

    <!-- 根据构造函数赋值
          index  代表参数的位置  从0开始计算
          type   指的是参数的类型在有多个构造函数时,可以用type来区分,要是能确定是那个构造函数,可以不用写type
          value  给基本类型赋值
          ref    给引用类型赋值
       -->
    <bean id="person_con" class="com.ysdrzp.di.Person">
        <constructor-arg index="0" type="java.lang.Long" value="1"></constructor-arg>
        <constructor-arg index="1" type="com.ysdrzp.di.Student" ref="student_con"></constructor-arg>
    </bean>
    <bean id="student_con" class="com.ysdrzp.di.Student"></bean>
    //默认构造函数
    public Person(){}
    
    //带参构造函数
    public Person(Long pid,Student students){
        this.pid = pid;
        this.students = students;
    }
    public class TestDI {
    
        @Test
        public void testSet(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            Person person = (Person) context.getBean("person");
            System.out.println(person.getLists());
        }
    
        @Test
        public void testConstrutor(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            Person person = (Person) context.getBean("person_con");
            System.out.println(person.getPid());
        }
    
    }

    总结:

    1、如果spring的配置文件中的bean中没有<constructor-arg>该元素,则调用默认的无参构造函数

    2、如果spring的配置文件中的bean中有<constructor-arg>该元素,则该元素确定唯一的构造函数

    作者:YSOcean
    本文版权归作者所有,欢迎转载,但未经作者同意不能转载,否则保留追究法律责任的权利。
  • 相关阅读:
    HDU5730 Shell Necklace
    BZOJ4883 [Lydsy2017年5月月赛]棋盘上的守卫
    Spring boot 配置文件
    org.joda.time.DateTime 日期操作
    Elasticsearch + Springboot 启动报错 java.net.ConnectException: Connection refused
    centos7 在docker下安装es Elasticsearch
    centos 7 。 docker 安装rabbitmq 以及集权搭建
    linux 安装mysql5.7.25
    安装rabbtimq CentOS 7
    docker + spring boot 打包 部署。
  • 原文地址:https://www.cnblogs.com/ysdrzp/p/9925565.html
Copyright © 2011-2022 走看看