Understanding the principles and main components of Spring3 IOC

Source: Internet
Author: User

? What is the IOC?

? Popularize understanding of the IOC principle

? IOC benefits

? Factory mode

? Main components of the IOC

? Application examples of IOC

? Attached: Instance code

1. What is the IOC (inversion of control)?

The core of the SPRING3 framework is the implementation of the inversion of Control (IOC) mode, which is also called Dependency Injection (DI).

What is control reversal? The answer is that "the process of acquiring dependent objects is reversed", and the process of acquiring dependent objects becomes an active injection by the IOC container, and control inversion is called Dependency injection. So "inversion of control" is simply "dependency injection"? In fact, we can understand "inversion of control" as a noun, and "dependency injection" as a verb, so "dependency injection" is used to achieve "control inversion" of a method, means. Dependency Injection is an IOC container that dynamically injects some kind of dependency into an object during operation, eventually decoupling the objects.

Popular image to understand the process of dependency injection: Lift computer (object a), USB device (object B), human (IOC container) example. As follows:

Use the computer (object a) and USB interface to implement: Computer (object A) the task of reading files from a USB device. Features: 1. Computer (object A) do not care what external devices are connected, such as a U-disk or a mobile hard disk, the computer (object a) can only passively accept The 2.USB device (object B) is OK if it complies with the USB standard; 3. When the computer (object a) requires an external USB device, needless to say, the person (IOC container) automatically hangs it. This is the process by which a USB device (object B) is injected into the computer (object a) while the system is running.

Thus, object A relies on object B, and when object a needs to use object B, the IOC container immediately creates an object B to object A. An IOC container is an object manufacturing plant, what you need, made for you and sent to you, you don't have to worry about anything, including how objects are generated and destroyed.

2. popularize understanding of IOC principles

The above has already introduced what is the control inversion, now further wordy. The principle of inversion is: the relationship between the control program by the container (before the program code is directly manipulated, that is, in a class call another class, that is, to use a class when the hand of the code new A class of objects come Out), "inversion" means "control" of the transfer, Control is relayed from the application code to the external container.

Here's a popular gear example to illustrate why you should use an IOC container to control the relationship between programs (if you don't understand what "decoupling" is, you'll see this example):

1. As can be seen from the figure below, the meshing of A, B, C, D four Gears form a gear group to accomplish a task. However, if there is a problem with a gear, it is likely to affect the normal operation of the entire gear set.

The relationship between gears in a gear group can be likened to the coupling between objects in a software system. The coupling between objects is unavoidable, as this is the basis for collaborative collaboration. But as the scale of the software becomes more and more, such as some industrial software, the dependencies between objects can be quite complex, often with multiple dependencies between objects. Coupling through high, the result will lead to "reaching" situation. Therefore, in order to realize the "decoupling" between the objects, the IOC theory is proposed.

2. The introduction of the IOC container, acting as a "binder", so that a, B, C, D four gears (objects) do not understand the coupling relationship, so that programmers can better division of labor to develop each module.

3.IOC Benefits

Here is also a continuation of the use of computer and U-disk examples to explain briefly:

1. Easy to decouple and simplify development: there is no correlation between the computer and the U-disk, regardless of which problem does not affect the other party. Also, because there is no coupling between the two components, the manufacturer of the build computer and the manufacturer that generated the U-disk can be different. In this way, a better division of labor can be developed.

2. Easy to test: Because of the independence between the computer and the U-disk, it is very convenient for unit testing, debugging and diagnosing separately. This is in line with the idea that the "good design is better than the specific implementation and the code should be easy to test" has been implemented by spring.

4. Factory mode

Factory model, as the name implies is the social industrialization brought about by the production division of products. The factory here refers to the place where the object is specifically produced, that is, declaring a class as a factory (factory Class) instance object used to generate other classes.

For example, a component requires the assistance of the B component without having to instantiate the B component on its own, but rather by producing a factory (beanfactory) of the B component.

Here is a factoryexample example to understand:

Code for Interface Person class: Person.java

+ View Code

Implement the code for the Interface class:

Chinese class: Chinese.java

+ View Code

American class: American.java

+ View Code

Factory class: Factory.java

+ View Code

Test class: Test.java

+ View Code

Operation Result:

 

The above Factoryexample example is a factory class to do object creation, in the spring IOC, we use the IOC container to complete the creation of objects, the specific code to see iocexample This example, The code for the Iocexample example is in the last attachment resource of the article.

5. Main components of the IOC

The two most basic and important packages of the SPRING3 framework:

1.org.springframework.beans.factory package (The primary interface of the package is beanfactory)

2.org.springframework.context package (The primary interface of the package is applicationfactory)

The main components of the SPRINGIOC framework are:

(1). Beans

(2). configuration file (Beans.xml or Applicationcontext.xml)

(3). Beanfactory interfaces and their related classes

(4). ApplicationContext interfaces and their related classes

Use the Iocexample2 example to understand the four main components of the SPRINGIOC framework:

(1). Beans

Beans refers to the bean that provides the business in the project, and the bean that the container manages, as the Chinese.java and American.java described above are javabean. Beans can contain properties and getter and setter methods that correspond to attributes, or other methods.

(2). configuration file (Beans.xml or Applicationcontext.xml)

Spirng3 managing beans through a configuration file, editing a configuration file This action is also called an "assembly Bean", where the assembly bean actually tells the container which beans are needed and how the container uses IOC to configure them.

The configuration file contains: The Bean's ID, class, attributes, and its values, a <beans> element and several <bean> child elements.

The Spring IOC framework takes the bean's class from the bean configuration file based on the Bean's ID, generates an object of that class, and then gets the properties and values of the object from the configuration file.

The following is a class diagram of the iocexample2 example:

The following is a configuration file for the Iocexample2 example (applicationcontext.xml): The following configuration file

123456789101112131415 <?xml version="1.0"encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN""http://www.springframework.org/dtd/spring-beans-2.0.dtd" [<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">]><beans>    <!--配置Bean,注入Chinese类对象-->    <bean id="中国人"class="iocexample2.Chinese">        <!--property元素用来指定需要容器注入的属性;name元素的指定属性值language;        ref元素指定需要向language属性注入的id,即注入的对象“英语”,该对象为Englis类生成-->        <property name="language"ref="英语"></property>    </bean>    <!--注入English-->    <bean id="英语"class="iocexample2.English"></bean></beans>

The results of the Iocexample2 example:

  

(3). Beanfactory interfaces and their related classes

(4). ApplicationContext interfaces and their related classes

Understanding the principles and main components of Spring3 IOC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.