Spring探究笔记01:IOC控制反转

481 阅读3分钟

相关代码:GithubGitee

一、IOC概述

1.1 什么是IOC

控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)

1.2 为什么要使用IOC

在日常程序开发过程当中,我们推荐面向抽象编程,面向抽象编程会产生类的依赖,当然如果你够强大可以自己写一个管理的容器,但是既然spring以及实现了,并且spring如此优秀,我们仅仅需要学习spring框架便可。 当我们有了一个管理对象的容器之后,类的产生过程也交给了容器,至于我们自己的app则可以不需要去关系这些对象的产生了。

为什么要把类生产的过程交给容器:有利于做一些通用的代码抽离,可以类比一下 代理设计模式

二、IOC的实现思路

  • 应用程序中提供类,提供依赖关系(属性或者构造方法)
  • 把需要交给容器管理的对象通过配置信息告诉容器(xml、annotation,javaconfig)
  • 把各个类之间的依赖关系通过配置信息告诉容器

三、IOC的实现方法

3.1 3中配置方式(编程风格)

虽然下面即将说到3种配置方式,但原理上,SpringIOC只有2中注入方法:基于setter注入、基于构造器注入。而所谓的3种配置方式(编程风格)只是不同的配置手段罢了。

3.1.1 xml风格

  • setter注入基础使用 目录结构如下:

    主要业务代码如下:

    可以看到,执行效果:
    This is indexService function.
    This is injectedString: 445566ddeeff
    This is index dao
    
  • 构造器注入基础使用
    1. 调整spring.xml文件如下:
      <bean id="indexService" class="cn.zephyr.IndexServiceImpl" >
          <constructor-arg name="indexDao" ref="indexDao"/>
          <constructor-arg name="injectedString" value="112233aabbcc"/>
      </bean>
      <bean id="indexDao" class="cn.zephyr.IndexDao"/>
      
    2. IndexServiceImpl添加全参、无参构造器的lombok注解
      @Data
      @AllArgsConstructor
      @NoArgsConstructor
      public class IndexServiceImpl implements IndexService {
          ...
      }
      
    3. 可以看到同样的效果
  • 添加p命名空间实现setter注入
    1. spring.xml文件添加p命名空间的约束:
      <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:context="http://www.springframework.org/schema/context"
              xmlns:p="http://www.springframework.org/schema/p"
              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
              ...
          </beans>
      </xml>
      
    2. 调整spring.xml文件如下:
      <bean id="indexService" class="cn.zephyr.IndexServiceImpl" 
        p:indexDao-ref="indexDao" p:injectedString="445566ddeeff"/>
      
    3. 可以看到同样的效果
  • 添加c命名空间约束实现构造器注入:
    1. spring.xml文件添加p命名空间的约束:
      <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:context="http://www.springframework.org/schema/context"
              xmlns:c="http://www.springframework.org/schema/c"
              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
              ...
          </beans>
      </xml>
      
    2. 调整spring.xml文件如下:
      <bean id="indexService" class="cn.zephyr.IndexServiceImpl"
        c:indexDao-ref="indexDao" c:injectedString="445566ddeeff">/>
      
    3. 可以看到同样的效果

3.1.2 注解风格

基于注解的方式可以说是极大的减轻了配置工作,使得我们能够更加专注于业务代码编写:

  1. 主要业务代码如下,执行后可以看到同样的效果:

3.1.1 java config风格

基于java config的方式,个人觉得让我们从繁琐的xml配置解放出来,使用java config可以提高代码可读性,减少出错: 主要业务代码如下(不再使用spring.xml,使用SpringConfig.java取代),执行后可以看到同样的效果: