控制反转(IOC)

控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。 -百度百科

作用:降低程序间的耦合性

Spring中的IoC

  1. 创建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">
    
</beans>
  1. 在xml中通过bean标签配置bean
<?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="accountService" class="com.oylong.service.impl.AccountServiceImpl"></bean>
    <bean id="accountDao" class="com.oylong.dao.impl.AccountDaoImpl"></bean>

</beans>
  1. 获取IoC容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml"); //bean.xml为你配置的xml文件
  1. 使用IoC容器获取对象
AccountService accountService =  applicationContext.getBean("accountService", AccountService.class);
//AccountService accountService =  (AccountService)applicationContext.getBean("accountService");
  • applicationContext的3个常用实现类

    1. ClassPathXmlApplicationContext:可以加载类路径下的配置文件,要求配置文件必须在类路径下,否则不能加载
    2. AnnotationConfigApplicationContext:用于读取注解,创建容器
    3. FileSystemXmlApplicationContext:可以加载磁盘任意有防有权限的配置文件
  • BeanFactoryApplicationContext的区别
    ApplicationContext:在构建容器时,创建对象采取的策略是立即加载的方式。读取完配置文件立马创建对象
    BeanFactory:采用延迟加载的方式创建对象,什么时候根据id获取对象,什么时候创建对象

Spring对bean对象对管理

  1. 创建bean对三种方式

    • 使用默认构造函数创建

      在Spring对配置文件中,使用bean标签配以idclass属性后,无其他属性和标签时,采用对就是默认构造函数创建bean对象,若对象中无默认构造函数,将无法创建
    • 使用某个类中的方法创建对象并存入Spring容器

      下面是某个类,通过方法获得一个AccountServiceImpl对象,如何通过这个方法将这个类配置到Spring容器中去?

      package com.oylong.factory;
      
      import com.oylong.service.impl.AccountServiceImpl;
      
      public class TestFactory {
          public AccountServiceImpl getService() {
              return new AccountServiceImpl();
          }
      }

      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="accountFactory" class="com.oylong.factory.TestFactory"></bean>
      
      <bean id="accountService" factory-bean="accountFactory" factory-method="getService"></bean>
      </beans>
    • 使用某个类中的静态方法创建对象并存入Spring容器

      这种方法更简便,如下Java类中有静态方法

      package com.oylong.factory;
      
      import com.oylong.service.impl.AccountServiceImpl;
      
      public class TestFactory {
          public static AccountServiceImpl getService() {
              return new AccountServiceImpl();
          }
      }

      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="accountService" class="com.oylong.service.impl.AccountServiceImpl"></bean> -->
      <bean id="accountService" class="com.oylong.factory.TestFactory" factory-method="getService"></bean>
            </beans>

bean对象的作用范围

  • 通过bean标签的scope属性来调整

    • singleton: 单例(默认值)
    • prototype:多例
    • request:作用于web应用的请求范围
    • session:作用于web应用的会话范围
    • global-session:作用于集群环境(全局)的会话范围,当不是集群环境时,与session范围相同

    常用的为单例和多例

bean对象的生命周期

  • 单例模式下:
    当容器创建时,对象出生,只要容器还在,对象就一直存在,直到容器被销魂,在单例情况下,对象的生命周期与容器相同。
  • 多例模式下:

    当使用对象时,Spring框架为我们创建对象,在对象的使用过程中,一直存在,当对象长时间不使用,且无其他对象使用时,由Java的垃圾回收机制自动回收。
Last modification:March 26, 2020
If you think my article is useful to you, please feel free to appreciate