Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework的依赖注入和控制反转

 Dependency Injection and Inversion of Control

 1.概述:

   1.1相关概念

      bean:由IoC容器所管理的对象,也即各个类实例化所得对象都叫做bean

      控制反转:原本是调用者决定自己要调用的对象,在调用者内部实例化被调用对象,控制被调用对象的生命周期。

           控制反转之后,IoC容器控制所有beans,在IoC容器中实例化得到各个对象并且放在该容器中进行管理,当发现有某个对象依赖其他对象时,由IoC

           容器执行依赖注入

      依赖注入:(其实和控制反转说的是一个东西),将被依赖对象注入到依赖对象中

      spring framework的IOC容器控制bean(即类对象,objects)的实例化、beans之间依赖关系的配置、bean的组装过程(依据依赖关系进行组装)

   1.2spring的IoC容器常被用来管理哪些实例化对象,也即objects(spring中又叫做bean)

      • (yes)service layer objects,
      • (yes)data access objects (DAOs),
      • (yes)presentation objects such as Struts Action instances,
      • (yes)infrastructure【基础结构】 objects such as Hibernate SessionFactories, JMS Queues, and so forth.
      • (no)Typically【通常】 one does not configure fine-grained domain objects in the container, because it is usually the responsibility of DAOs and business logic to create and load domain objects. 

 2.如何使用spring framework的依赖注入和控制反转功能:

      2.1涉及到的包:

        • org.springframework.beans     (springframework的the )
        • org.springframework.context

      2.2包中最主要的两个类(接口):

        • BeanFactory接口       (一般不使用该接口)
        • org.springframework.context.ApplicationContext接口 (编程中一般使用该接口)  
            • ApplicationContext接口就代表spring的IoC容器
            • ApplicationContext接口(IoC容器)负责 实例化、配置、组装 项目中出现的各个类对象(beans)   
            • 该接口又有若干实现类,实际编程时可能要使用ApplicationContext接口的若干实现类来编写代码
        • 附录一: 上述两个接口的关系
            • ApplicationContext接口实际上是BeanFactory接口的子接口,所以前者比后者有更多的功能,所以编程过程中一般使用ApplicationContext接口来编程,即一般通过 ApplicationContext接口 获取并使用spring framework的IoC容器和依赖注入功能        

       2.3实际编程思路:编程过程中如何配置beans之间的依赖关系

        • 概述:使用IoC容器的时候,需要对依赖关系进行说明,有三种办法配置各个bean之间的依赖关系,一种是通过XML配置文件,另外一种是通过注解代码来配置各个对象(bean)之间的依赖关系,还有一种是使用Java代码实现一个专门的讲述依赖关系的配置类(该*.java专门讲述依赖关系)
        • 方法一:XML-based configuration,使用XML文件配置beans之间的依赖关系。
            •  XML-based configuration metadata shows these beans configured as <bean/> elements inside a top-level <beans/> element.  
            • example: shows the basic structure of XML-based configuration metadata,即使用xml文件声明各个bean之间的依赖关系   
        • 方法二:Annotation-based configuration,使用annotion配置各个beans之间的依赖关系
        • 方法三:Java-based configuration,
            • Java configuration typically uses @Bean annotated methods within a @Configuration class.   
        • (不建议使用)除了可以通过上述三种配置方法将bean交给spring的IOC容器进行管理之外,还可以将在容器之外生成的objects交给spring容器进行管理,具体操作方法如下:
            • In addition to bean definitions that contain information on how to create a specific bean, the ApplicationContext implementations also permit the registration of existing objects that are created outside the container, by users. This is done by accessing the ApplicationContext’s BeanFactory via the method getBeanFactory() which returns the BeanFactory implementation DefaultListableBeanFactoryDefaultListableBeanFactory supports this registration through the methodsregisterSingleton(..) and registerBeanDefinition(..). However, typical applications work solely with beans defined through metadata bean definitions. 
        • 小结:程序中可能同时使用了annotion-based、xml-based、Java-based configuration,当这几种配置代码同时存在时,spring IOC 容器会按照以下次序依次执行相应的配置信息
            • 先执行annotion-based configuration
            • 再执行xml-based configuration
            •           

 

      2.4实际编程思路:编程过程配置beans之间的依赖关系时要注意选择beans实例化的方法

        • 概述,实例化beans有三种方法,分别是通过构造函数实例化bean,通过类中的static成员方法实例化bean,通过另外一个类中的非static成员函数实例化bean.下面以xml-based configuration为例讲解“ 配置beans之间的依赖关系时如何配置beans 的实例化方式”
        • 方法一,Instantiating bean with a constructor【构造函数】
        • 方法二,
                          

 

3.编程思路(使用IOC/DI功能)

3.1概述

如下图:要选择开发过程中你想要使用的实例化bean的方法、DI的方法、编写依赖关系配置代码的方法

    •     

3.2实际使用实例

    • example1:The following example uses通过无参构造函数实例化bean、 XML-based configuration metadata for setter-based DI.  详细解说:通过无参构造函数 return an instance of the object,并且beans的依赖关系是在XML文件中配置的,最后,选定了通过构造函数参数给bean的各个属性赋初始值的方法进行DI。
    • <bean id="exampleBean" class="examples.ExampleBean">
          <!-- setter injection using the nested ref element -->
          <property name="beanOne">
              <ref bean="anotherExampleBean"/>
          </property>
      
          <!-- setter injection using the neater ref attribute -->
          <property name="beanTwo" ref="yetAnotherBean"/>
          <property name="integerProperty" value="1"/>
      </bean>
      
      <bean id="anotherExampleBean" class="examples.AnotherBean"/>
      <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
      public class ExampleBean {
      
          private AnotherBean beanOne;
          private YetAnotherBean beanTwo;
          private int i;
      
          public void setBeanOne(AnotherBean beanOne) {
              this.beanOne = beanOne;
          }
      
          public void setBeanTwo(YetAnotherBean beanTwo) {
              this.beanTwo = beanTwo;
          }
      
          public void setIntegerProperty(int i) {
              this.i = i;
          }
      
      }
    • example2:The following example uses XML-based configuration metadata for  constructor-based DI.  通过构造函数实例化得到bean

      <bean id="exampleBean" class="examples.ExampleBean">
          <!-- constructor injection using the nested ref element -->
          <constructor-arg>
              <ref bean="anotherExampleBean"/>
          </constructor-arg>
      
          <!-- constructor injection using the neater ref attribute -->
          <constructor-arg ref="yetAnotherBean"/>
      
          <constructor-arg type="int" value="1"/>
      </bean>
      
      <bean id="anotherExampleBean" class="examples.AnotherBean"/>
      <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
      public class ExampleBean {
      
          private AnotherBean beanOne;
          private YetAnotherBean beanTwo;
          private int i;
      
          public ExampleBean(
              AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
              this.beanOne = anotherBean;
              this.beanTwo = yetAnotherBean;
              this.i = i;
          }
      
      }
    •  example3: call a static factory method to return an instance of the object,使用xml-based configuretion配置beans之间的依赖关系,  constructor-based DI对bean实例的各个属性进行初始化赋值

      <bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance">
          <constructor-arg ref="anotherExampleBean"/>
          <constructor-arg ref="yetAnotherBean"/>
          <constructor-arg value="1"/>
      </bean>
      
      <bean id="anotherExampleBean" class="examples.AnotherBean"/>
      <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
      public class ExampleBean {
      
      //没有相应的属性
      //也没有含有三个参数的构造函数
      //xml文件中construtor-arg子标签所指定的值赋值给本class中static factory-method 的参数
      //Arguments to the static factory method are supplied via <constructor-arg/> elements, 
      // a private constructor private ExampleBean(...) { ... } // a static factory method; the arguments to this method can be // considered the dependencies of the bean that is returned, // regardless of how those arguments are actually used. public static ExampleBean createInstance ( AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) { ExampleBean eb = new ExampleBean (...); // some other operations... return eb; } }
       

             

学习的过程中总会得到一些心得体会,认真地将它们记录下来并分享给每一个愿意花费时间去阅读它们的人,然后意外地收获某个读者的评论,从而激发出新的感想,是一件十分令人欢快的事。如果你也在研习这方面的知识,欢迎加入到我们的队伍中来,和我们一起进步吧(^_^)
原文地址:https://www.cnblogs.com/lxrm/p/6242694.html