Writing in the front
Anyone who has written Java knows that all objects must be created; Or: objects must be created before they can be used. With IOC, instead of creating objects manually, you can fetch them directly from the IOC container.
It’s as if we don’t have to worry about object destruction collection because the Java garbage collection mechanism helps us do this; Ioc, on the other hand, eliminates the need for object creation and allows the IOC container to implement object creation, injection, etc.
Inversion of control
The spring ioc container
There are only three Core components in the Spring framework: Core, Context, and beans. They build the skeleton of Spring, without which AOP, Web, and other features would not be possible.
If there is one more core out of the three, it is beans. Spring is bean-oriented programming, so to speak, and beans are the real heroes in Spring.
Why is Spring so popular? You’ll find that Spring solves a key problem by allowing you to manage relationships between objects using configuration files, or annotations, its dependency injection mechanism. This injection relationship is managed in a container called Ioc. An Ioc container is an object wrapped around a Bean. Spring manages objects and does a list of additional operations by wrapping them in beans.
Core components work together
BeanFactory vs. ApplacationContext
The core interface of IOC is Beanfactory, which provides IOC’s high-level services. ApplicationContext is based on Beanfactory, which provides abstract application-oriented services.
Three injection methods
In the Spring framework, the dependency injection (DI) design pattern is used to define the dependencies of objects on each other. In the case of XML configuration beans, there are two main types:
- Setter method injection
- Constructor injection
Of course, with annotations, the way to use annotations is much easier and faster. That is, the autowire function implements automatic property injection (@autowire).
Writing here, reminds me of a multiple-choice question I read on niuke.com recently:
Which of the following is wrong about Spring dependency injection? A. Dependency injection is usually of the following two types: set injection and construct injection: Constructor injection determines the injection order of dependencies in constructors, with priority given to dependency injection. C. Construct injection is performed first, followed by set injection when both exist. D. Set injection means that the IoC container uses setter methods of properties to inject dependent instances. This injection method is relatively simple and intuitiveCopy the code
The answer from Niuke is C, but it seems that netizens are divided on the answer. Check out your comments and answers
The principle of analytic
The Spring code is really hard to read, too detailed and hard to describe in text. I read other people’s blogs about it, posted a lot of code, and drew a lot of ER diagrams to describe the relationships between key interfaces or classes. Such a long article down, we may not read the code carefully, look at the ER diagram, simply do not follow the trend. I just posted a little key code in my opinion, heh heh.
Initialization of the context
When you run the ApplicationContext CTX = new ClassPathXmlApplicationContext (” the applicationcontext.xml “); The constructor ClassPathXmlApplicationContext (String configLocation) call this (new String [] {configLocation}, true, null); , the constructor code is as follows.
Start the above initialization from a sequence diagram
Write in the last
The next article will write about Spring MVC, again in mind mapping style. Visual learning, so that Java is no longer difficult.
Finally, please follow my wechat official account Java-MindMap to download the source files, as well as more Java mind maps and project materials for you to learn. Every three or four days, I will update an article, bringing you into the world of memorizing brain maps.
Read the previous article
- Spring Mind Map, make Spring not difficult (1)