Spring's IOC principle

Source: Internet
Author: User

Before using spring to do a small project, suddenly feel that he is very convenient and useful, specifically to study the framework, all of a sudden it was attracted by its ingenious!

First, let's take a look at spring's IOC core features:

(1) What is called IOC and di?

A: IOC refers to the control inversion, why is it controlled inversion? Because the so-called control refers to the control of the specific implementation class for the interface, inversion refers to the transfer of this control to a third party or an external class, so the obvious benefit is that it greatly reduces the complexity of the program and allows the programmer to focus more on the business logic, let's take a look at an example:

public class Theater{public void Setorder () {//Here the words Li this person directly into the Setorder this action, because originally we just want to waiter this role to accept the order, There should be no definite candidate waiter waiter = new Li (); Waiter.say ("OK");}}

So it should be better to use an external class to determine waiter specific object, but let waiter to implement the order of the plot

DI: Represents the injection of dependent objects at run time. Because ApplicationContext this spring container initializes all beans at the beginning of the instance of the bean, so when you configure dependencies in XML or annotation, The spring container uses the Beanwrapper component to set the dependencies and values between the beans through three injection methods using Java reflection.

(2) In what form does the resources in spring exist?

A: Resources in spring, such as Classpath,file,url, are implemented with a resource interface, and all resources are resource specific implementation classes, Spring uses a resourceloader to read resource, and then generates Beandefiniton, each beandefinition corresponding to a bean node, and then placed in a registry.

(3) There are several spring containers, what are their differences?

A: In general, we call beanfactory an IOC container, and ApplicationContext is a spring container, and the application context is a container above the beanfactory, providing more functionality.

There is also a special webapplicationcontext for the Web tier that allows the initialization to be done from the assembly configuration file relative to the Web root path (SPRINGMVC is the typical representative inside). You can get the ServletContext reference (request,response,session), which is to place the entire WEB application context object as a property in ServletContext, with a root_web_application The _context_attribute constant is the key, and the application context is placed in the ServletContext attribute table.

We can use Contextconfiglocation to start the Web container:

<context-param><param-name>contextconfiglocation</param-name><param-value>/web-inf/ Bao-dao.xml</param-value></context-param><listener><listener-class> Org.springframework.web.context.contextloaderlistener</listener-class></listener>
The biggest difference between beanfactory and ApplicationContext is that the application context automatically uses the Java reflection mechanism to sweep the registry of the surface Beandefinition, sweep the post processor's beans out and use the Java reflection mechanism to automatically register the number, Beanfactory, however, must be registered by hand using Addbeanpostprocesser ().

(4) What does the spring configuration mean?

For:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"//Default space name, Refers to <bean> this xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/ Schema/p "P is an alias, the following string is the full name xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-3.0.xsd                Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-3.0.xsd                Http://www.springframework.org/schema /mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd                http://www.springframework.org/schema/ Util http://www.springframework.org/schema/util/spring-util-3.0.xsd ">    behind this schemalocation is the schema file above, The IDE can reference the schema file to provide guidance for document editing

(5) How does the spring container work?

A: I don't know why the mind map can't be passed up.



(1) First Use resourceloader load configuration file into resource class, then use Beandefinitionreader to read resource object, get a bunch of raw beandefinition, and then put into a registry, Take Beanid as key

(2) Call the factory after the processor, there are two main functions, one is to replace the placeholder in the beandefinition with the real value, that is, it will load the external file, the value of the inside load into the container, and then compared with the placeholder, and then set it in, The second is to use Java's reflection mechanism to scan a propereditor bean and put it in a registry.

(3) is the initialization bean, at this time the bean has not set the value, by Beanwrapper set the value in

(4) Put a ready bean instance into spring's buffer pool or, if it is a prototype, directly to the caller

Spring's IOC principle

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.