Write in front
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:
下面有关spring的依赖注入,说法错误的是? A、依赖注入通常有如下两种:设置注入和构造注入: B、构造注入可以在构造器中决定依赖关系的注入顺序,优先依赖的优先注入 C、当设值注入与构造注入同时存在时,先执行构造注入,再执行设值注入 D、设值注入是指IoC容器使用属性的setter方法来注入被依赖的实例。这种注入方式比较简单、直观
New Ket Net The answer is to choose c, but netizens seem to have different views of the answer ha. View comments and Answers
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
The refresh () method. jpg starts the above initialization from the time series view
Container initialization sequence diagram. jpg
Java Mind Mapping
Links: http://www.jianshu.com/p/a6144d2035a8
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Spring Mind Map (IOC article)