IOC与DI的理解及使用

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

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

IOC演示:

  1. UserDao接口

    public interface UserDao {
    void getUser();
    }
  2. UserDaoImpl实现类

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

    public interface UserService {
    void getUser(); void setUserDao(UserDao userDao);
    }
  4. 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>
    1. 参数名赋值
    <bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg name="years" value="7500000"/>
    <constructor-arg name="ultimateAnswer" value="42"/>
    </bean>
    1. 根据类型赋值(有同类型参数时则会有问题)
    <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 + '\'' +
    '}';
    }
    }
  2. 测试对象

    @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;
    }
  3. 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>
  4. 测试类

    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的作用域

Scope Description
singleton (Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
prototype Scopes a single bean definition to any number of object instances.
request 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.
session Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
application Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocket Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
  1. 单例模式(Spring默认机制)

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

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

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

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

最新文章

  1. EF架构~CodeFirst生产环境的Migrations
  2. jxl写入excel实现数据导出功能
  3. dwz的form表单中url的变量替换
  4. MVC5 + EF6 + Bootstrap3 (8) HtmlHelper用法大全(上)
  5. 如何使用impress.js做一个网页版本的PPT
  6. 20150914 异常语句 math的方法 去空格 索引
  7. C++常用语法
  8. 更改DataTable列名方法
  9. USACO Section 1.1-3 Friday the Thirteenth
  10. [Code] 递归函数在函数式 Java 中的实现
  11. Codeforces Round #424 Div2 E. Cards Sorting
  12. 找jar包的网站 还没用过2017.12.19
  13. oracle里实例和数据库之间的关系
  14. C# 解决“请求被中止: 未能创建 SSL/TLS 安全通道”的问题
  15. 即时通讯(IV)
  16. 日常工作--IIS修改增加最大数据库连接池
  17. nodejs----http系统模块使用和fs系统模块
  18. flask-前台布局页面搭建3
  19. 百度api--之导航
  20. python 中变量引用问题

热门文章

  1. Spring Boot 2.x基础教程:使用EhCache缓存集群
  2. javascript : 递归遍历数组
  3. layui :iframe 与 layer 的位置问题
  4. StringBuffer类和StringBuilder类
  5. 关于页面布局中,如何让一个div水平和垂直居中的五个方案
  6. 3分钟看懂Python后端必须知道的Django的信号机制!
  7. 究竟什么时候该使用MQ?
  8. Merging 和 Rebasing 的大比拼
  9. Django学习路4_数据库添加元素,读取及显示到网页上
  10. PHP ezmlm_hash() 函数