Make spring no longer difficult to understand-IOC article

Source: Internet
Author: User

Java is known to all: All objects must be created, or they must be created before using the object. With IOC, you can no longer create objects manually, but instead get objects directly from the IOC container.

It's like we don't have to think about object destruction, because the Java garbage collection mechanism helps us implement this process, and the IOC lets us not consider the object creation process, which the IOC container helps us to create, inject, and so on.

Spring knowledge details. png

Control reversal

Introduction to Spring IOC. png

Spring IOC Container

There are only three core components in the Spring Framework: core, context, and Bean. They build the entire spring skeleton architecture, without which there is no way to have AOP, web and other features.

Spring overall structure diagram. jpg

If a core is chosen in three cores, it is not a bean. It can be said that spring is a bean-oriented programming, beans in spring is the real protagonist.

Why is spring so popular? You will find that spring solves a very critical problem, which allows you to switch between the object and the configuration file to manage, or annotate, its dependency injection mechanism. This injection relationship is managed in a container called IOC. The IOC container is the object that is wrapped by the bean. Spring is the purpose of managing these objects and doing some extra work by wrapping the objects in the bean.

IOC container. png

Spring IOC initialize. png

How the spring IOC container works. jpg

Core components work together

Three core components. png

The difference between beanfactory and Applacationcontext

The most central interface in the IOC is Beanfactory's Advanced services for IOC, and ApplicationContext is an application-oriented service built on beanfactory basis.

Beanfactory and Applacationcontext.png

3 Methods of Injection

In the spring framework, the design pattern of Dependency Injection (DI) is used to define the dependencies between objects. In the case of XML configuration beans, it has two main types:

    • Setter Method Injection

    • Constructor injection

Of course, with annotations, the way to use annotations is more convenient and quick. Automatic assembly function for automatic attribute injection (@autowire).

Writing here, reminds me of a recent choice on the Ox-guest online to see:

The following is about spring's dependency injection, what's wrong with that? A, dependency injection usually has the following two kinds: Set injection and construct injection B, the construction injection can determine the injection order of dependency in the constructor, priority injection    C, when the value injection and the construction injection exist simultaneously, the first to perform the construction injection, then perform the setpoint injection  D, SetPoint injection refers to an IOC container that uses a setter method of attributes to inject a dependent instance. This kind of injection method is relatively simple, intuitive network of the answer is selected C, but netizens seem to have different views of the answer ha. View comments and answers by netizens (https://www.nowcoder.com/questionTerminal/a1644e2668f94ab78bfefa38808fb006)
Principle Analysis

Spring code is really hard to read, too thin, the text is difficult to describe, read someone else about the blog, paste a lot of code, draw a lot of ER diagram to describe the key interface or the relationship between classes. Such a long article down, we may not seriously read the code, look at the ER diagram, simply do not follow suit. Just put a little bit in my opinion of the key code, hey.

The initialization process of the context

When the ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); constructor method ClassPathXmlApplicationContext(String configLocation) Call this(new String[] {configLocation}, true, null); is run, the constructor method is as follows.

Classpathxmlapplicationcontext construction method. jpg

Refresh () method. jpg

Start the above initialization from the time series diagram

Container initialization sequence diagram. jpg

Reference Link: Https://mp.weixin.qq.com/s/TQvmt84imz77oTcH7KQ1rQ

Make spring no longer difficult to understand-IOC article

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.