Spring IOC and AOP and dynamic proxy in AOP

IoC understanding

Personally think the easiest paragraph to understand.
Quoting Bromon's blog
IoC and DI, I
want to talk about IoC ( Inversion of Control ). This is the core of spring, throughout. The so-called IoC, for the spring framework, is that spring is responsible for controlling the life cycle of objects and the relationship between objects . What does this mean, for a simple example, how do we find a girlfriend? The common situation is that we go everywhere to see where there are beautiful and good-looking mms, and then inquire about their hobbies, qq number, phone number, ip number, iq number..., find a way to get to know them, and join them. Good to give what we want, and hehe... This process is complicated and profound, we must design and face each link by ourselves. The same is true for traditional program development. In an object, if you want to use another object, you must get it (new one yourself, or query one from JNDI), and then destroy the object after use (such as Connection, etc.). Objects will always be coupled with other interfaces or classes.
  So how does IoC do it? It's a bit like finding a girlfriend through a matchmaking agency. A third party was introduced between my girlfriend and me: a marriage agency. The matchmaking agency manages the information of a lot of men and women. I can submit a list to the matchmaking agency and tell it what kind of girlfriend I want to find, such as Li Jiaxin, Lin Xilei, singing like Jay Chou, speed like Carlos, technology like Qi Dane and the like, then the matchmaking agency will provide a mm according to our requirements, we just need to fall in love with her and get married. Simple and straightforward, if the person given to us by matchmaking does not meet the requirements, we will throw an exception. The whole process is no longer controlled by myself, but a container-like organization like matchmaking. This is the way of development advocated by Spring.All classes will be registered in the spring container, tell spring what you are and what you need, and then spring will actively give you what you want when the system is running at an appropriate time, and also hand you over to other needs. your things. The creation and destruction of all classes are controlled by spring , which means that it is no longer the object that references it, but spring that controls the life cycle of an object. For a specific object, it used to control other objects, but now all objects are controlled by spring, so this is called inversion of control. If you still don't understand, I decided to give up.
A key point of IoC is to dynamically provide an object with other objects it needs while the system is running . This is achieved through DI (Dependency Injection) . For example, object A needs to operate the database. In the past, we always had to write code in A to obtain a Connection object. With spring, we only need to tell spring that a Connection is needed in A. As for how and when this Connection is constructed, A does not need to know. When the system is running, spring will create a Connection at an appropriate time, and then inject it into A like an injection, thus completing the control of the relationship between each object. A needs to rely on Connection to run normally, and this Connection is injected into A by spring. That's how the name of dependency injection comes from. So how is DI implemented? An important feature after Java 1.3 is reflection , which allows programs to dynamically generate objects, execute methods of objects, and change properties of objects when the program is running. Spring implements injection through reflection. Please refer to the java doc for information about reflection.

Three injection methods of Spring IOC:

1. Interface injection through the class contains a certain interface
2. getter, setter method injection through the set method in the class
3. Constructor injection class construction method parameters are injected
4 annotation injection @Autowried @Resource @Service...

AOP

The picture is very clear.
AOP dynamic proxy
reference https://blog.csdn.net/u011277123/article/details/89206722
Spring's AOP proxy is still inseparable from Spring's IOC container. The generation, management and dependencies of the proxy are all made by The IOC container is responsible. Spring uses JDK dynamic proxy by default. When a proxy class is needed instead of a proxy interface, Spring will automatically switch to using CGLIB proxy. However, current projects are all interface-oriented programming, so JDK dynamic proxy is relatively used Still more.
Spring will choose the dynamic proxy mode according to whether the specific Bean has an interface. If there is an interface, the dynamic proxy mode of Jdk is used. If there is no interface, the dynamic proxy mode of cglib is used.

Guess you like

Origin blog.csdn.net/weixin_43663421/article/details/109221403