Overview
The spirng-aop module is the core module in the Spring framework. Although the Spring Ioc container does not rely on AOP, AOP provides a powerful and flexible solution to the implementation of Ioc.
In Spring Framework, AOP is mainly used for two purposes:
From a functional perspective, AOP may be regarded as a complement to the OOP programming method, providing a different way of code or system organization. The core concept in OOP is Class, and in AOP is Aspect.
The spirng-aop module is the core module in the Spring framework. Although the Spring Ioc container does not rely on AOP, AOP provides a powerful and flexible solution to the implementation of Ioc.
In Spring Framework, AOP is mainly used for two purposes:
Spring AOP is implemented in pure Java, and does not require special compilation processing and does not require control of the hierarchy of the class loader, so it can be used for Servlet Container and other application servers.
Spring AOP currently only supports method-level switching or interception, and attribute interception is not supported now. If you want to intercept attributes, you can consider using AspectJ language.
Spring AOP is used differently from most other AOP frameworks. Its main purpose is not to provide a large and comprehensive set of AOP implementations, but to integrate different AOP implementations and collaborate with Spring Ioc to help solve some common problems.
It should be noted that some fine-grained advised (such as domain model), Spring AOP often does not provide good support, and this scenario still considers AspectJ. Even so, in general experience, Spring AOP's powerful mechanism can still solve the problems in most scenarios.
So how should we view Spring AOP and AspectJ, quoting the original text of Spring's official documents:
Spring AOP will never struggle to compete with AspectJ to provide a comprehensive AOP solution. We believe that both proxy-based frameworks like Spring AOP and full-blown frameworks such as AspectJ are valuable, and that they are complete, rather than in competition. Spring seamlessly integrates Spring AOP and IoC with AspectJ, to enable all uses of AOP to be catered for within a consistent Spring-based application architecture. This integration does not affect the Spring AOP API or the AOP Alliance API: Spring AOP remains backward-compatible.
In all Spring framework module designs, one of the core tenets that is always adhered to is non-invasive.
Therefore, when using Spring AOP, we will not force us to introduce specific classes or interfaces into the business code, which can keep the code clean and decouple to the maximum extent. However, Spring also provides another option, if there is a specific scenario, you can directly introduce Spring AOP in your code. Almost all modules in Spring frameworks will give you a variety of choices in the way they are used so that users can choose a way that is more suitable for their scenarios. Use AspectJ or Spring AOP, use annotation or xml configuration method, Depends On U.
After understanding the original intention and usage scenarios of Spring AOP, let’s take a look at its general implementation principle
Most problems in the software world can be solved by adding a layer.
The layer mentioned here is of course in a broad sense, which can be an abstraction or a cache, which roughly means the category of isolation and decoupling.
In the Spring world, the introduction of each module, or the integration of third-party technologies, will always provide an abstraction layer, providing a unified API to users, blocking all implementation details and differences between different implementations. For example, modules such as spring-cache, spring-jdbc, spring-jms and spirng-messaging all provide a layer of abstraction.
Spring AOP implementation is a proxy-based mechanism, which uses Jdk dynamic proxy by default, and can also use cglib proxy. The difference between the two is mainly the difference between the objects being proxied. When the target object is an interface, Jdk dynamic proxy can complete the proxy, but when the target object does not implement the interface class (try to be less, interface-oriented programming is a good habit), it is necessary to use cglib proxy to complete the proxy. Of course, you can also force the interface to use cglib as a proxy; in addition, when specific types need to be injected or referenced, if the referenced object is exactly the proxy object, you also need to use cglib method.
Functional design and implementation can be divided into two major parts
Creation of AOP
The core class that generates the proxy object, ProxyFactoryBean getObject()
The following figure is the selection logic of whether to use Jdk or cglib when generating a proxy:
After finding the specific executor of the generation agent, when is this operation called? Those who have understood the life cycle of Spring beans should know that when a bean is created, there are a series of callback interfaces for users to insert custom behaviors to influence some of the bean's characteristics. BeanPostProcessor is one of the interfaces. Previous articles have been introduced (the ultimate weapon to play with Spring beans). Spring AOP took advantage of this opportunity to insert a trick in the process of creating a bean. If the bean being created is our aop target, create a proxy and finally return the proxy object to Ioc.
AbstractAutoProxyCreator This class is an implementation of BeanPostProcessor, used to create a proxy, see the post-processing method of this processor, and finally returns the proxy returned by the createProxy() method.
Enhanced execution of AOP sections
It can be understood as a call to all interceptor chains on the target object
Since there are two specific implementations of Spring AOP, JDK dynamic proxy and cglib, the methods of executing interceptors are different. For details, you can read the source code JdkDynamicAopProxy invoke method
The call to the target method ultimately relies on ReflectiveMethodInvocation.
The process processing in ReflectiveMethodInvocation uses recursive method to process interceptor chains.
Intercept method of CglibAopProxy
CglibMethodInvocation inherits ReflectiveMethodInvocation, and the process() method above is used to handle interceptor chains.
Two details to note when using Spring AOP:
1. Spring AOP does not work when calling the method inside the class (self-invoke), because the internal call does not pass the proxy object and is directly used. Solutions are:
2. When injecting beans, if you want to inject the specific type of bean instead of the interface, then use cglib
Spring AOP has powerful functions and clever design. The main context is sorted out here, and the details will not be discussed one by one.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.