(Essay) Spring IOC

1. What is Spring IOC?
The translation of IOC into Chinese is inversion of control . In layman's terms, it means transferring the work of creating objects and managing object relationships from programmers to Spring.
2. What is the purpose?
Reduce coupling between classes.
3. If there is a UserSevice class and a UserDao class now, if you want to call a method of UserDao in the UserSevice class, how to do it?
3.1. Traditional way:
Create a new UserDao object in the UserSevice class, and then call its method.
3.2. Factory mode:
Design a factory class, create a new object in the factory class and return it, UserSecieve only needs to call this method of the factory class.
3.3.IOC (using xml + factory mode + reflection)
write an xml file, configure the information of the class to be created, then design a factory class, parse the class name configured in the xml file in the factory class, and then use the reflection mechanism , create an object with the class name, and return it.

Compare the three methods: the first method has a high degree of coupling, although the second method uses the factory mode, it is actually a new object in the factory class, with a high degree of coupling, and the third method uses the reflection mechanism. The degree of coupling is minimal.

The first one: If the class path of the latter changes, the former needs to modify the code, and the coupling degree is very high. The third way: If you modify the path of the latter class, you only need to change the path information of the configuration file. The degree of coupling is low.

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/PaperJack/article/details/124515801