"Dead Spring"-----IOC's deep understanding of spring IOC

Source: Internet
Author: User

When we first learned about spring, we contacted IOC, and as spring's first core concept, we must have a deep understanding of it before we read it, this is the first blog post of the "Dead Spring" series, which focuses on the basic concepts and components of the IOC.

IOC theory

IoC is all called Inversion of Control , translated as "inversion of control", and it has an alias of DI ( Dependency Injection ), which is dependency injection.

How to understand "inversion of control" good? The key to understanding it is that we need to answer the following four questions:

    1. Who Controls who?
    2. Control what?
    3. Why the reversal
    4. Which aspects reversed the

Before answering these four questions, let us first look at the definition of IOC:

The so-called IOC is the Spring IOC container that is responsible for the relationship between the object's life cycle and the object.

The above statement is at the heart of the entire IoC theory. How to understand this sentence? Let's cite an example to illustrate (the four questions above are not a problem).

have been looking for a girlfriend for example (this is a question worth exploring for a program ape). In general, how do we find a girlfriend? First we need to according to their own needs (beautiful, good shape, good character) to find a sister, and then around to inquire about her interests and hobbies, telephone numbers, and then all kinds of feed to send it, and finally chase. As follows:

/** * 年轻小伙子 */public class YoungMan {    private BeautifulGirl beautifulGirl;    YoungMan(){        // 可能你比较牛逼,指腹为婚        // beautifulGirl = new BeautifulGirl();    }    public void setBeautifulGirl(BeautifulGirl beautifulGirl) {        this.beautifulGirl = beautifulGirl;    }    public static void main(String[] args){        YoungMan you = new YoungMan();        BeautifulGirl beautifulGirl = new BeautifulGirl("你的各种条件");        beautifulGirl.setxxx("各种投其所好");        // 然后你有女票了        you.setBeautifulGirl(beautifulGirl);    }}

This is the way we usually do things, and if we need an object, it's usually done in such a straightforward way ( new BeautifulGirl() ), it's complicated and tedious, and we have to face every step, and we're going to have to destroy it when it's finished. In this case our object is coupled with the object it depends on.

Actually, we need to think about a problem? Do we really need to create it ourselves every time we use the objects we rely on? We know that we rely on the object is not relying on the object itself, but rely on it to provide services, as long as we need it, it can provide services in a timely manner, as it is our initiative to create or others sent to us, in fact, is not so important. Moreover, compared to their own hardships to create it also to manage, aftercare, direct someone sent over is not it seems better?

The "man" that gives us something is the IoC, in the example above, it's the equivalent of a matchmaking company, which manages a lot of men and women as a matchmaking company, and when we need a girlfriend, we ask our needs directly with the matchmaking company, Matchmaking companies will be based on our needs to provide a sister to us, we only need to be responsible for love, the birth of monkeys on the line. You see, this is not very simple and clear.

Admittedly, as a matchmaking company, the IoC helped us to omit the complex process of looking for a girlfriend, the original active search into the current passive acceptance (in line with our requirements), more concise and light. You want Ah, originally you have to horse, all kinds of flattery, what things need their own to pro-force, now good, direct someone put out the ready to send over, how wonderful things ah. So, to put it simply, the idea of the IoC is to let others serve you , such as (excerpted from spring):

When the IOC is not introduced, the injected object is directly dependent on the object being relied upon, and with the IOC, both and their relationships are managed and maintained by the IOC Service Provider. What the injected object needs, directly with the IOC service Provider, the latter will inject the corresponding dependent objects into the injected object, so that the IOC service Provider for the purpose of being injected into the object. so the IoC is so simple! It turns out you need something to get it for yourself, and now it's something that needs to be sent to someone else (IOC Service Provider).

Now looking at the four questions above, the answer is obvious:

    1. who controls who : in the traditional development model, we are all using the direct new object to create the object, that is, you rely on the object directly from your own control, but with the IOC container, it is directly controlled by the IOC container. So "who controls who", of course, is the IoC container control object.
    2. control what : Control object.
    3. Why reverse : When there is no IoC, we are actively creating dependent objects in our own object, which is a positive turn. However, with the IOC, the dependent objects are injected into the injected object directly by the IOC container, and the dependent objects are reversed by the original active acquisition into passive acceptance.
    4. which aspects are reversed : The acquisition of the dependent object is reversed.

Sister has, but how to have a sister? It is also a subject of learning.

    1. Maybe you're a little more pulp, just when you were born.
    2. In most cases we will still consider what kind of sister we want, so we still need to greet the matchmaking company.
    3. The other thing is, you don't even know what kind of sister you want, just tell the matchmaking company that I want a sister like that.

Therefore, the IOC Service Provider provides the dependent objects for the injected object also in the following ways: Construct method injection, Stter method injection, interface injection.

Constructor injection

constructor injection, as the name implies, is injected by declaring the argument list of the dependent object in its construction method, letting the outside know which dependent objects it needs.

YoungMan(BeautifulGirl beautifulGirl){        this.beautifulGirl = beautifulGirl;}

The constructor injection method is more intuitive, the object construction can be used directly, it is like you were born in your home to give you the designation of your daughter-in-law.

Setter Method Injection

For JavaBean objects, we generally use getter and setter methods to access and set the properties of an object. Therefore, the current object only needs to provide a corresponding setter method for the object on which it depends, so that the appropriate dependent object can be set to the injected object by this method. As follows:

public class YoungMan {    private BeautifulGirl beautifulGirl;    public void setBeautifulGirl(BeautifulGirl beautifulGirl) {        this.beautifulGirl = beautifulGirl;    }}

Setter injection will be looser and more flexible than the constructor inject, it can be injected at any time (before using dependent objects, of course), which is like you can first think of the sister you want, and then greet the matchmaking company, you can have Lin Chi-ling style, editorialcommittee style, Even sister Feng which, random strong.

Interface Mode injection

Interface mode injection is more overbearing, because it requires the object to be relied upon to implement unnecessary interfaces, with intrusive features. This method is not generally recommended.

On the IOC theory part, the author is not elaborated, here are recommended several blog reading:

    • Talk about the spring IOC Understanding: http://www.cnblogs.com/xdp-gacl/p/4249939.html
    • Spring's IOC principle [popular explanation]:7802126
    • Spring IOC principle (you can write your own spring after reading): 4402245
Individual components

Look first (excerpt from: http://singleant.iteye.com/blog/1177358)

The graph is Classpathxmlapplicationcontext's class inheritance architecture, though only partially, but it basically contains most of the core classes and interfaces of the IOC system.

Here's a simple split and supplemental explanation for this diagram.

Resource system

Resource, an abstraction of resources, each of its implementation classes represents a resource access strategy, such as Classpathresource, Urlresource, Filesystemresource, and so on.

With resources, you should have the resources to load, Spring uses Resourceloader to load the uniform resources, the class diagram is as follows:

Beanfactory system

Beanfactory is a very pure bean container, it is an IOC necessary data structure, where beandefinition is her basic structure, it maintains a beandefinition map internally, and according to Beandefinition description The creation and management of beans.

The Beanfacoty has three direct subclasses ListableBeanFactory , HierarchicalBeanFactory and AutowireCapableBeanFactory , for the DefaultListableBeanFactory final default implementation, it implements all interfaces.

Beandefinition system

Beandefinition is used to describe Bean objects in Spring.

Beandefinitionreader system

The role of Beandefinitionreader is to read the contents of the Spring configuration file and convert it into a data structure inside the IOC container: beandefinition.

ApplicationContext system

This is the well-known Spring container, which is called the application context, which is closely related to our application, she inherits Beanfactory, so it is beanfactory extended upgrade version, if beanfactory is the cock silk, then ApplicationContext is a veritable Gaofu. Since the structure of ApplicationContext determines its difference from the beanfactory, the main differences are:

    1. Inherit Messagesource, providing an internationalized standard access policy.
    2. Inherit Applicationeventpublisher, providing a powerful event mechanism.
    3. Extended Resourceloader, which can be used to load multiple Resource, provides flexible access to different resources.
    4. Support for Web apps.

Source: 47043143

The above five systems can be said to be the most central part of the Spring IoC, the following blog post is also for the five parts of the source code analysis. In fact, the IoC is very simple to look at, is nothing more than the configuration file (think of the XML file) for parsing (analysis of XML who will not AH), and then put into a Map inside the almost, at first look at the truth, in fact, there are a lot of problems, the following will be bothered you spectators follow the LZ Blog to uncover the mysteries of the Spring IoC step-by-step.

This series of blog for LZ Learning, research Spring mechanism and the source of learning notes, will involve reference to other people's blog and book content, if there is a similar, purely for reference, of course, LZ will indicate the source of reference. At the same time, due to the knowledge and ability of the problem, the article will inevitably appear wrong, if there is, hope you guys pointed out, greatly appreciated .
LZ write this series of blog, Spring the latest version is 5.0.6.RELEASE, so this series blog all source source is 5.0.6.RELEASE.

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.