Spring——IOC(控制反转)与DI(依赖注入)


IOC与DI的理解及使用

控制反转IOC(Inversion of Control)是一种设计思想,DI(依赖注入)是实现IOC的一种方法 。在没有IOC的程序中,我们使用面向对象编程,对象的创建于对象间的依赖完全硬编码在程序中,对象的创建有程序自己控制;控制反转后将对象的创建转移给第三方;

控制反转是一种 通过描述XML(或注解)通过第三方去生产或获取特定对象 的方式。 在Spring中实现控制反转的是IOC容器 ,其 实现方法是依赖注入 (Dependency Injection,DI)

IOC演示:

  1. UserDao接口
public interface UserDao {
        void getUser();
    }
  1. UserDaoImpl实现类
public class UserDaoImpl implements UserDao{
        public void getUser() {
            System.out.println("默认获取用户");
        }
    }
  1. UserService业务接口
public interface UserService {
        void getUser();

        void setUserDao(UserDao userDao);
    }
  1. UserServiceImpl业务实现类
public class UserServiceImpl implements UserService {

        private UserDao userDao ;

        //利用set进行动态实现值的注入
        public void setUserDao(UserDao userDao){
            this.userDao = userDao;
        }

        public void getUser() {
            userDao.getUser();
        }
    }

这里使用一个Set方法实现

private UserDao userDao ;

          //利用set进行动态实现值的注入
          public void setUserDao(UserDao userDao){
              this.userDao = userDao;
          }

          //代替了原先主动在该类中穿创建对象:
          //userDao = new UserDao();
  • 之前,程序是主动创建对象,控制权在开发者手上!
  • 使用set注入后,程序不再具有主动性,而是变成了被动的接收对象!
  • 这种对象的控制权的转变思想就是IOC(控制反转)

这种思想,从本质上解决了问题,程序员不用再去管理对象的创建了,系统的耦合性大大降低,可以更加专注在业务的实现上。这就是IOC的原型。

1、第一个spring

Spring的核心就是基于IOC容器,将对象的创建以及依赖的注入全部交由Spring容器来完成。

首先基于如上接口和业务创建两个接口实现类:

public class UserDaoMysqlImpl implements UserDao{
        public void getUser() {
            System.out.println("mysql获取用户");
        }
    }



    public class UserOracleImpl implements UserDao{
        public void getUser() {
            System.out.println("oracle获取用户");
        }
    }

创建applicationContext.xml文件

基于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
            https://www.springframework.org/schema/beans/spring-beans.xsd">

        <!--一个bean就对应一个实例化一个对象,在Spring中管理-->
        <bean id="mysqlImpl" class="com.spong.dao.UserDaoMysqlmpl"/>
        <bean id="oracleImpl" class="com.spong.dao.UserOracleImpl"/>

        <!--userServiceImpl实例注入ioc容器-->
        <bean id="userServiceImpl" class="com.spong.service.UserServiceImpl">
            <!--
                ref:引用Spring容器中创建好的对象
                value:具体的值,基本数据类型
            -->
            <!--给实例化对象中的属性赋值,自动调用类中的setUserDao()方法,没有或者方法名不规范则会出错-->
            <!--ref传入哪个实现类的id,就调用userServiceImpl的set方法注入这个实现类-->
            <property name="userDao" ref="mysqlImpl"/>
        </bean>

    </beans>

测试:

public class MyTest {
        public static void main(String[] args) {
            //获取Spring的上下文对象ApplicationContext
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

            //对象已经在Spring中管理了,使用时直接根据bean id取即可
            UserServiceImpl serviceImpl = (UserServiceImpl) context.getBean("userServiceImpl");
            serviceImpl.getUser();

        }
    }

现在,我们彻底不用在程序中去改动了,要实现不同的操作,只需要在XML中进行修改;

这个过程就叫控制反转:

  • 控制:谁来控制对象的创建,传统的应用程序是由程序本身控制创建的,使用Spring后,对象由Spring来创建;
  • 反转:程序本身不创建对象,而变成被动的接受对象;
  • 依赖注入:就是利用set、构造方法等进行属性的注入;
    • 依赖:bean对象的创建依赖于容器!
    • 注入:bean对象中的所有属性,由容器来注入!

IOC是一种编程思想,由主动的编程变为被动的接收。

总结为一句话:对象由Spring来创建,管理,装配。

2、IOC创建对象的方法

  1. 默认使用无参构造创建对象

  2. 自定义使用有参构造创建对象

    1. 下标赋值

      <bean id="exampleBean" class="examples.ExampleBean">
          <constructor-arg index="0" value="7500000"/>
          <constructor-arg index="1" value="42"/>
      </bean>
    2. 参数名赋值

<bean id="exampleBean" class="examples.ExampleBean">
            <constructor-arg name="years" value="7500000"/>
            <constructor-arg name="ultimateAnswer" value="42"/>
        </bean>
3. 根据类型赋值(有同类型参数时则会有问题)
<bean id="exampleBean" class="examples.ExampleBean">
            <constructor-arg type="int" value="7500000"/>
            <constructor-arg type="java.lang.String" value="42"/>
        </bean>

总结:在配置文件加载的时候,容器中管理的对象就已经初始化了。

3、DI(依赖注入)

IOC的一个重点是在系统运行中,动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的。

DI的实现原理是反射(reflection),它允许程序在运行的时候动态的生成对象、执行对象的方法、改变对象的属性,Spring就是通过反射来实现注入的。

理解DI的关键是:“谁依赖谁,为什么需要依赖,谁注入谁,注入了什么”,那我们来深入分析一下:

  • 谁依赖于谁 :是 应用程序依赖于IoC容器

  • 为什么需要依赖应用程序需要IoC容器来提供对象需要的外部资源

  • 谁注入谁 :是 IOC容器注入应用程序依赖的某个对象

  • 注入了什么 :就是 注入某个对象所需要的外部资源 (包括对象、资源、常量数据)。

1、构造器注入

  1. 默认使用无参构造创建对象

  2. 自定义使用有参构造创建对象:

    1. 下标赋值
<bean id="exampleBean" class="examples.ExampleBean">
        <constructor-arg index="0" value="7500000"/>
        <constructor-arg index="1" value="42"/>
    </bean>
2. 参数名赋值
<bean id="exampleBean" class="examples.ExampleBean">
        <constructor-arg name="years" value="7500000"/>
        <constructor-arg name="ultimateAnswer" value="42"/>
    </bean>
3. 根据类型赋值(有同类型参数时则会有问题)
<bean id="exampleBean" class="examples.ExampleBean">
        <constructor-arg type="int" value="7500000"/>
        <constructor-arg type="java.lang.String" value="42"/>
    </bean>

*2、set方式注入

【环境搭建】

  1. 复杂类型
public class Address {
        private String address;

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        @Override
        public String toString() {
            return "Address{" +
                    "address='" + address + '\'' +
                    '}';
        }
    }
  1. 测试对象
@Data
    public class Student {
        private String name;
        private Address address;
        private String[] books;
        private List<String> hobbies;
        private Map<String,String> card;
        private Set<String> games;
        private String wife;    //null
        private Properties info;
    }
  1. beans.xml
<bean id="address" class="com.spong.pojo.Address">
            <property name="address" value="zhejiang"/>
        </bean>

        <bean id="student" class="com.spong.pojo.Student">
            <!--基本类型注入 value-->
            <property name="name" value="ps"/>

            <!--bean对象注入 ref-->
            <property name="address" ref="address"/>

            <!--数组-->
            <property name="books">
                <array>
                    <value>西游记</value>
                    <value>三国演义</value>
                    <value>红楼梦</value>
                    <value>水浒传</value>
                </array>
            </property>

            <!--list-->
            <property name="hobbies">
                <list>
                    <value>唱歌</value>
                    <value>打游戏</value>
                </list>
            </property>

            <!--map-->
            <property name="card">
                <map>
                    <entry key="学生证" value="147596654"/>
                    <entry key="身份证" value="33216541236542512"/>
                </map>
            </property>

            <!--set-->
            <property name="games">
                <set>
                    <value>LOL</value>
                    <value>CS</value>
                </set>
            </property>

            <!--properties-->
            <property name="info">
                <props>
                    <prop key="性别"></prop>
                    <prop key="年龄">18</prop>
                </props>
            </property>

            <!--null-->
            <property name="wife">
                <null/>
            </property>
        </bean>
  1. 测试类
public class MyTest2 {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            Student student = (Student) context.getBean("student");
            System.out.println();
        }
    }
  • 测试结果
Student{
        name='ps', 
        address=Address{address='address'},
        books=[西游记, 三国演义, 红楼梦, 水浒传], 
        hobbies=[唱歌, 打游戏],
        card={学生证=147596654, 身份证=33216541236542512},
        games=[LOL, CS],
        wife='null', 
        info={性别=男, 年龄=18}
    }

总结:在配置文件加载的时候,容器中管理的对象就已经初始化了。

3、拓展

可以使用p命名空间和c命名空间进行注入:

p命名(properties)

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--带可选参数名称的传统声明-->
        <bean name="classic" class="com.example.ExampleBean">
            <property name="email" value="someone@somewhere.com"/>
        </bean>

        <!--带参数名称的p-名称空间声明-->
        <bean name="p-namespace" class="com.example.ExampleBean"
            p:email="someone@somewhere.com"/>
    </beans>

c命名(constructor)

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:c="http://www.springframework.org/schema/c"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean id="beanTwo" class="x.y.ThingTwo"/>
        <bean id="beanThree" class="x.y.ThingThree"/>

        <!-- 带可选参数名称的传统声明 -->
        <bean id="beanOne" class="x.y.ThingOne">
            <constructor-arg name="thingTwo" ref="beanTwo"/>
            <constructor-arg name="thingThree" ref="beanThree"/>
            <constructor-arg name="email" value="something@somewhere.com"/>
        </bean>

        <!-- 带有参数名称的c-名称空间声明 -->
        <bean id="beanOne" class="x.y.ThingOne" 
            c:thingTwo-ref="beanTwo"
            c:thingThree-ref="beanThree" 
            c:email="something@somewhere.com"/>

    </beans>

注意:p命名和c命名空间不能直接使用,需要导入xml约束

xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:c="http://www.springframework.org/schema/c"

4、bean的作用域

<table>  
<tr>  
<th>

Scope

</th>  
<th>

Description

</th> </tr>  
<tr>  
<td>

[singleton](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-
framework-reference/core.html#beans-factory-scopes-singleton)

</td>  
<td>

(Default) Scopes a single bean definition to a single object instance for each
Spring IoC container.

</td> </tr>  
<tr>  
<td>

[prototype](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-
framework-reference/core.html#beans-factory-scopes-prototype)

</td>  
<td>

Scopes a single bean definition to any number of object instances.

</td> </tr>  
<tr>  
<td>

[request](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-framework-
reference/core.html#beans-factory-scopes-request)

</td>  
<td>

Scopes a single bean definition to the lifecycle of a single HTTP request.
That is, each HTTP request has its own instance of a bean created off the back
of a single bean definition. Only valid in the context of a web-aware Spring
`ApplicationContext`.

</td> </tr>  
<tr>  
<td>

[session](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-framework-
reference/core.html#beans-factory-scopes-session)

</td>  
<td>

Scopes a single bean definition to the lifecycle of an HTTP `Session`. Only
valid in the context of a web-aware Spring `ApplicationContext`.

</td> </tr>  
<tr>  
<td>

[application](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-
framework-reference/core.html#beans-factory-scopes-application)

</td>  
<td>

Scopes a single bean definition to the lifecycle of a `ServletContext`. Only
valid in the context of a web-aware Spring `ApplicationContext`.

</td> </tr>  
<tr>  
<td>

[websocket](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-
framework-reference/web.html#websocket-stomp-websocket-scope)

</td>  
<td>

Scopes a single bean definition to the lifecycle of a `WebSocket`. Only valid
in the context of a web-aware Spring `ApplicationContext`.

</td> </tr> </table>
  1. 单例模式(Spring默认机制)

bean的一个共享实例,并且所有对具有ID或与该bean定义相匹配的ID的bean的请求都会导致该特定的bean实例由Spring容器返回

<!--scope标签修改作用域-->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
  1. 原型模式:

每次对特定bean提出请求时,bean部署的非单一原型范围都会创建一个新的bean实例

<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
  1. 其余的request、session、application等只能在web开发中使用,对应的就是这三类作用域。


原文链接:https://www.cnblogs.com/spang/p/13472236.html