[Spring -AOP-IOC definition]

IoC(Inversion of Control)

(1). IoC (Inversion of Control) refers to the relationship between container control program objects, not in traditional implementation, which is directly controlled by program code. Control is transferred from the application code to the external container, and the transfer of control is the so-called reversal. For Spring, it is Spring that controls the life cycle of objects and the relationship between objects; IoC has another name-"Dependency Injection". Understand from the name, the so-called dependency injection, that is, the dependency between components is determined by the container at runtime, that is, the container dynamically injects a certain dependency into the component.
(2). In Spring's working method, all classes will be registered in the spring container, telling spring what it is and what you need, and then spring will take the initiative to do what you want when the system is running properly. To you, but also to other things that need you. 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.
(3). During the operation of the system, dynamically provide an object with other objects it needs.
(4). The idea of ​​dependency injection is realized through the reflection mechanism. When instantiating a class, it uses reflection to call the set method of the class to inject the class attributes stored in the HashMap into the class. All in all, in the traditional method of object creation, usually the caller creates the instance of the callee, and the work of creating the callee in Spring is done by Spring, and then the caller is injected, which is the so-called dependency injection or control reaction. turn. There are two injection methods: dependency injection and setting injection; the advantages of IoC: reduce the coupling between components, reduce the complexity of replacement between business objects, and enable it to manage objects flexibly.

AOP(Aspect Oriented Programming)

(1). AOP aspect-oriented programming is based on IoC, which is a useful supplement to OOP;
(2). AOP uses a technique called "cross-cutting" to dissect the inside of the encapsulated object and affect those The public behavior of the class is encapsulated into a reusable module and named "Aspect", which is the aspect. The so-called "aspect", in simple terms, is to encapsulate the logic or responsibilities that are not related to the business but are called by the business modules, such as log records, which is convenient to reduce the repetitive code of the system and reduce the coupling between modules. Conducive to future operability and maintainability.
(3). AOP represents a horizontal relationship, which compares the "object" to a hollow cylinder, which encapsulates the properties and behavior of the object; the method of aspect-oriented programming is to use this cylinder in the form of a section. Dissected and selectively provide business logic. The cut section is the so-called "aspect". Then it restored these cut planes with ingenious skill, leaving no traces, but achieving the effect.
(4). The technology to achieve AOP is mainly divided into two categories: one is the use of dynamic proxy technology, which uses the method of intercepting the message to decorate the message to replace the execution of the original object behavior; the second is the use of static weaving In this way, a specific grammar is introduced to create "aspects", so that the compiler can weave code about "aspects" during compilation.
(5). Spring implements AOP: JDK dynamic proxy and CGLIB proxy JDK dynamic proxy: its proxy object must be the realization of an interface, which is to complete the proxy of the target object by creating an implementation class of the interface during operation; its The two core classes are InvocationHandler and Proxy. CGLIB proxy: The implementation principle is similar to the JDK dynamic proxy, except that the proxy object it generates during runtime is a subclass extended for the target class. CGLIB is an efficient code generation package. The bottom layer is implemented by ASM (open source java bytecode editing class library) to manipulate bytecode, and its performance is stronger than JDK; it needs to introduce packages asm.jar and cglib.jar. The aspects driven by AspectJ injection and @AspectJ annotations are actually implemented at the bottom level through dynamic proxies.
(6). AOP usage scenarios:
Authentication Permission Check
Caching Cache
Context passing Content Delivery
Error handling Error handling
Lazy loading Lazy loading
Debugging Debugging
logging, tracing, profiling and monitoring Logging, tracking, optimization, calibration
Performance optimization Performance optimization, efficiency check
Persistence Persistence
Resource pooling Resource pool
Synchronization synchronization
Transactions transaction management
In addition, the realization of Filter and the realization of the interceptor of struts2 are the embodiment of AOP thought.

Guess you like

Origin blog.csdn.net/dong8633950/article/details/114750396