The implementation of Spring source--IOC container--a review __spring

Source: Internet
Author: User

Spring is often used in projects where an interview was asked how spring loaded the bean, and I answered it by Java reflection, without hesitation. But the interviewer is skeptical, make me a bit blind, look back is also read a lot of information. Preliminary read the source code of the next SPRINGIOC, with a large number of template mode, good ni forgive, I do not see the special understanding, but the basic ability to understand the principles of the IOC container and thought. Write it down. "Ps:mod 20160809 (Valentine's Day), a recent study of spring source, has made changes to the article, as a review of the IOC" IOC (Control reversal) and Di (Dependency injection) IOC (control reversal) IoC (inversion, control inversion). This is the core of spring, throughout. The so-called IOC, for the spring framework, is that spring is responsible for controlling the life cycle of objects and the relationships between objects.
This is how spring is being pioneered, all classes are registered in the spring container, telling spring what you are and what you need, and spring will take the initiative to you when the system is in place and give you what you need. All classes are created and destroyed by spring, which means that the control object's life cycle is no longer the object that references it, but spring. For a specific object, it controls other objects, and now all objects are spring-controlled, so this is called a control reversal. DI (Dependency injection) One of the highlights of the IOC is that it dynamically provides the other objects it needs to an object in a system run. This is achieved by DI (Dependency injection, Dependency injection). For example, object A needs to manipulate the database, we always have to write code in a to get a connection object, with spring we just need to tell spring,a need a connection, as to how this connection constructs, when constructs , a does not need to know.

The relationship between the IOC and DI, in fact, is a different perspective of the same concept, because the concept of control reversal is rather vague (perhaps just as a layer of container control objects, it is difficult to think of who will maintain the object relationship), so the 2004 master-class figure Martin Fowler also gave a new name: "Dependency Injection", in contrast to the IOC, "Dependency Injection" explicitly describes "the injected object relies on IOC container configuration dependent objects." The initialization of the container for the load process of the Spring Bean does a few things: the resource is positioned to locate your configured XML file to parse the resource file and parse it into the spring-defined Beandefinition Registering with Beandefinition is actually equivalent to putting beandefinition into a hashmap-dependent injection: an instance of the bean being injected depending on the beandefinition creation Bean instance

The following figure is very intuitive to see: (from:http://blog.csdn.net/cutesource/article/details/6132650)


Here to add that the article started the interview question, it should be said that spring to instantiate the bean in a variety of ways, read the source can be found:[mod:20160813] 1. The factory method creates the bean,2. Constructor method creates parameters, 3. There is lookup, The Replace-method Bean uses cglib bytecode and the rest is reflected in Java.

Spring's inheritance System "20160809"

Description: Based on the Spring3.0.5 version.

This interface system is centered on beanfactory and applicationcontext . Beanfactory is the most basic interface of the IOC container, ApplicationContext on the one hand inherits the Beanfactory system: Listablebeanfactory and Hierachicalbeanfactory, Has the basic function of Beanfactory IOC container, on the other hand, by inheriting Messagesource, Resourcepatternresource, Environmentcapable, Applicationeventpublisher these interfaces, with more advanced IOC container features.  
the process of spring in a project

First, the Web project uses spring to initialize the IOC container by configuring Org.springframework.web.context.ContextLoaderListener inside the Web.xml.

<!--instantiate spring container--> <listener> <listener-class> at startup Org.springframework.web.context.contextloaderlistener</listener-class> </listener>

Contextloaderlistener inherits the Contextloader and implements the Servletcontextlistener interface. When the server container (commonly referred to Tomcat) is started, an event initialization is received. Initwebapplicationcontext method is in the Org.springframework.web.context.ContextLoader class, it does a few things: first of all, to determine whether the ServletContext has been registered Webappli Cationcontext, if any, throw an exception to avoid duplicate registration. The log is then enabled and the timer is started. Then a series of complex things, set the ServletContext into Xmlwebapplicationcontext properties, This allows you to read the spring file that is configured in the Web.xml through Xmlwebapplicationcontext, and then to perform the instantiation of the IOC container above and the injection of the bean.

The source code reading Tips "20160809" above for spring's several diagrams and simple explanation still can see, but spring's source code read not so good-looking, here give everybody a suggestion: along the main line to follow the code, and then look back to the auxiliary code. because spring is a very mature framework, it has a lot to think about in the coding process, such as the launch of the IOC container, we just need to know how it locates resource, how to parse beandefinition, how to register. In this process, there will be many considerations based on security, performance, scalability, and so on, and spring uses a lot of design patterns, and it produces a lot of the redundancy in our eyes, do not know what to do code. So, the first few times to read the source code, to be able to put aside these codes follow the main line of the code has been watching, and so you make the process clear, and then look back to some of the previous auxiliary code.

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.