I. What is IOC
IOC is inversion of control, leaving the process of object creation and calls between objects to Spring to manage, making code less coupled.
In the previous article, we used the creation of XML managed objects, which is the implementation of IOC.
Ii. Underlying principles of IOC
IOC implementation mainly uses three technologies: factory schema, XML parsing, reflection.
1. Factory model introduction
The original state
So let me graph what the initial object call looks like. For example, if I want to call the Add () method of the UserDao class in the UserService class, I need to new a UserDao object.
But this invocation method makes the UserService and UserDao classes too tightly coupled. For example, when the UserDao path changes, the UserService changes as well. In the spirit of high cohesion and low coupling, we need to further reduce the coupling of the program.
The factory pattern
Thus, in order to better decouple, the factory pattern emerged. After adding the factory, the above object call relationship looks like this:
Reduce the coupling between the UserService and UserDao classes through the UserFactory factory class.
However, the UserFactory factory is also a class, and the coupling with the other classes is still there, so this is still not the final solution. If the coupling is further reduced, the IOC scheme is used.
XML parsing is used to retrieve or manipulate the contents of a file. Reflection, on the other hand, does this by taking the bytecode file of the class, which is the compiled class file of the Java file that was written, and then manipulating everything in that class.
2. IOC implementation process
The first step
Configure objects through XML files. Let’s say I configure a User object in a file.
The second step
Once you have the XML file, IOC can parse the file to get the object and then create a factory class to return the object.
The graphic code only indicates:
- Get the value of the class attribute by parsing the XML.
- Through reflection, objects are created.
- Create object, return.
The same factory mode is used, but the coupling degree is further reduced through IOC. Because even if the path of the User class changes and it is not “com.pingguo.spring5.user”, you only need to change the XML file, and no other code needs to be changed.
3. IOC interface
The IOC container is essentially an object factory, and in Spring, there are two ways to implement the IOC container (interfaces). In the test code in the previous article, I replaced the ApplicationContext with the BeanFactory and it worked just as well.
1. BeanFactory
The interfaces used internally in Spring are generally not available to us developers. Its features are:
- Objects are not created when the configuration file is loaded; they are created when objects are used.
2. ApplicationContext
The BeanFactory is a subinterface of the ApplicationContext that provides more and more power for us developers.
If you are using the IDEA editor, Ctrl+ left-click BeanFactory and then click the ↓ arrow in front of BeanFactory to see the parent interface implemented.
Its features are the opposite of BeanFactory:
- When the configuration file is loaded, the configuration object is created.
3. The two main implementation classes of ApplicationContext
You can click ApplicationContext and press Ctrl+H to view the structure.
- ClassPathXmlApplicationContext: in the last example code, the use of this class. The feature is that you can write the path of the XML file under SRC.
- FileSystemXmlApplicationContextThe full path of the file on disk is used here, for example
D:\IdeaProjects\spring5_demo\src\bean1.xml
.
4. BeanFactory or ApplicationContext?
At first glance, BeanFactory feels better, because I need to create it when I use it to save resources.
However, from a real production perspective, Spring is usually applied to Web projects, such as in conjunction with Tomcat. So when Tomcat is started, we prefer to deal with these time-consuming and resource-consuming operations when the project is started, which will be more appropriate.