Spring Framework IOC and dependency inversion control Detailed injection DI

This article describes the Inversion of Control IOC Spring framework and dependency injection DI, welcome to read, learn together and progress together.

Spring Framework Reference basis : in-depth study Spring basis



I. Introduction entry procedures and IOC

  • 依赖注入或控制反转的定义中,调用者不负责被调用者的实例创建工作The work to be responsible by the Spring Framework container it to determine the instance type configuration by developers to create after the injection of the caller.
  • Since the Spring container is responsible for the callee instance, and then create an instance of the instance is responsible for injecting the caller, hence the name 依赖注入.
  • Created a working example of the caller no longer be created by the caller but be created by Spring, control is transferred from the application code to the outside of the container, control reversal occurred, hence the name 控制反转.

Here Insert Picture Description


Two .IOC- Inversion of Control

  • IOC是 Inverse of Control 的简写,意思是控制反转. It is to reduce the coupling between objects of design.
  • By IOC, developers do not need to be concerned about the process of creating an object, to complete the Spring container. Specifically process, Spring program reads the configuration file, the object needs to acquire bean created instance of an object created by the reflection mechanism.
  • Disadvantage : the object is instantiated by the reflection mechanism is, so have a certain impact on the performance of the system.
  • advantage:
  • 内存控制: Unified managed object, the object to avoid the chaos created result in additional memory overhead. Facilitates the optimization of the memory.
  • 降低耦合度: Scalable project, easy to maintain. If the next IoC + interfaces case, delete any implementation classes will not cause compile errors. While running to get a specific error code, but the code will not have a problem ----- other from the side also reflects a loosely coupled in use.

Here is a simple example: you are a tenant to rent an apartment, IOC is equivalent to the housing agency, he was responsible for managing all of the houses, he gives you all the information listings objects already registered you are looking for. If what you need will provide what.

Here Insert Picture Description


Three .DI- Dependency Injection

  • Dependency Injection,说的是创建对象实例时,同时为这个对象注入它所依赖的属性。Equivalent to the relationship between each of the bean and bean to the container management . And this container is spring.
  • For example : We usually instances it depends Dao layer Service injection layer; examples Service Controller layer in the implanted layer.

Here Insert Picture Description


Four. IOC and DI

  • IOC alias, in 2004, Martin Fowler discusses the same problem, since the IOC is Inversion of Control, then in the end is "what is inverted control of it?" After a detailed analysis and feasibility studies, he came to answer: 获得依赖对象的过程被反转了" ." After the control is reversed by the process of obtaining their dependent objects managed by the object becomes active injection IOC container. So he "inversion of control" took a more appropriate name is "dependency injection (Dependency Injection, DI)".他的这个答案,实际上给出了实现IOC的方法:注入。
  • The so-called dependency injection, that is, by the IoC container during operation, dynamic dependencies injected into certain objects.
  • Therefore, dependency injection (DI) and reverse control (IOC) describe the same thing from different angles, it refers to the introduction by IOC container by way of injection dependency, decoupling between objects .

Five .Spring framework uses (IoC)

  • 1. Create a JavaBean
  • 2. JavaBean configuration will be managed to the Spring container
<bean id="book" class="com.it.spring.beans.Book">
    <property name="bookId" value="1"/>
    <property name="bookName" value="Java程序设计"/>
    <property name="bookPrice" value="20.20"/>
    <property name="publishTime">
        <bean class="java.util.Date"/>
    </property>
</bean>
  • 3. initialization Spring container
  • Spring loaded core profile applicationContext.xml
ClassPathXmlApplicationContext context =    new ClassPathXmlApplicationContext("applicationContext.xml");
  • 4. Getting Object Spring container
Book book = (Book)context.getBean("book");

  • Spring IoC principle
  • ① Spring loaded first core configuration file by ClassPathXmlApplicationContext class
  • ② core configuration file parsing applicationContext.xml, the bean configuration information
  • ③ When we get the object from the container, Spring bean id of the container will create objects by the reflection technique under and complete injection property

VI. Types of injection in three ways

  • ①set method
  • By the completion of the injection property tag attribute value is actually calling the property set methods corresponding to
  • When injecting property, simple value types, and strings can be directly specified by the value attribute
  • There are two types of objects can be injected ways :
<property id="..." name="objAttr">
    <bean class="package.Class"/>
</property>
<bean id="objRef" class="package.Class"/>
<property id="..." name="objAttr" ref="objRef"></property>
  • ② constructor
  • It is in the constructor (the order of the parameters may be determined by the index index = 0,1)
<bean id="book3" class="com.it.spring.beans.Book">
    <constructor‐arg>
        <value>3</value>
    </constructor‐arg>
    <constructor‐arg value="phython程序设计"/>
    <constructor‐arg value="44.44" />
    <constructor‐arg ref="date"></constructor‐arg>
</bean>
  • May be configured to specify the order parameter by the index attribute, if there is no index attribute, all the parameters must match the order of the parameters in the class constructor.
  • ③ interface injection

  • The best investment for young people is to invest in yourself
    Here Insert Picture Description
  • 2020.03.25 来自辰兮的第36篇博客
Published 40 original articles · won praise 182 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_45393094/article/details/105095982