How spring ioc works

Source: Internet
Author: User

How spring ioc works

I used spring to do a small project. I thought it was very convenient and useful. I studied this framework and it was suddenly attracted by its ingenuity!

First, let's take a look at the core features of spring's ioc:

(1) What are ioc and di?

A: ioc refers to the control reversal. Why is it a control reversal? Because the so-called control refers to the control of the specific implementation class of the interface, the reversal refers to the transfer of this control to a third party or an external class, one obvious benefit of this is that it greatly reduces the complexity of the program and enables programmers to focus more on business logic. Let's take a look at an example below:

 

Public class Theater {public void setOrder () {// here, li directly intruded into the setOrder action, because we originally wanted the role of waiter to accept the order, you should not determine the specific candidates for Waiter waiter = new Li (); waiter. say (OK );}}

Therefore, it is better to use an external class to determine the target object of the waiter, but to let the waiter accept the order.

 

DI: Indicates injecting dependency objects at runtime. The spring container applicationContext initializes all beans at the beginning when initializing bean instances. Therefore, when you configure dependencies in xml or annotation, the spring container uses beanWrapper, which uses java reflection to set the dependencies and values between beans in three injection modes.

(2) In spring, how do resources exist?

A: All resources in spring, such as classpath, file, and url, implement a resource interface, and all resources are specific implementation classes of resources. spring uses a resourceLoader to read resources, beandefiniton is generated. Each beanDefinition corresponds to a bean node and is then placed in a registry.

(3) What are the differences between spring containers?

A: In general, beanFactory is called an ioc container, ApplicationContext is a spring container, and the application context is a container on beanFactory, providing more functions.

There is also a special webApplicationContext for the web layer, which allows you to complete initialization from the assembly configuration file in the path relative to the web root directory (springmvc is a typical example ), we can get the reference of ServletContext (request, response, session), which places the entire web application context object as the attribute in ServletContext, and uses a ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE constant as the key, the value of the application context is placed in the Attribute Table of ServletContext.

We can use contextConfigLocation to start the web container:

 

 
  
   contextConfigLocation
  
  
   /WEB-INF/bao-dao.xml
  
 
 
  
   org.springframework.web.context.ContextLoaderListener
  
 
The biggest difference between BeanFactory and ApplicationContext is that the application context will automatically use the java reflection mechanism to scan the Beandefinition registry, scan the post-processor bean, and use the java reflection mechanism to automatically register the number, beanFactory must manually use addBeanPostProcesser () for registration.

 

(4) What does spring configuration mean?

A:

 
 
  
This xmlns: xsi = http://www.w3.org/2001/XMLSchema-instance xmlns: p = drawing 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> the schemaLocation behind is the schema file above, and 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 cannot be uploaded.

 


 

(1) first use resourceLoader to load the configuration file into a resource class, then use beanDefinitionReader to read the resource object, get a bunch of unprocessed beanDefinition, and then put it into a registry, with beanID as the key

(2) Call the factory postprocessor. There are two main functions: one is to replace the placeholder in beanDefinition with a real value, that is, it will load external files, load the value pairs in the container, compare them with placeholders, and set them again, the second is to use the java reflection mechanism to scan the bean of properEditor and put it in a registry.

(3) initialize the bean. At this time, the bean has not set the value. beanWrapper sets the value.

(4) Add the ready bean instance to the spring buffer pool or directly send it to the caller if it is a prototype.

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.