What is said upstairs is not accurate either. It still starts with
AOP Alliance and Specification . You can see two things under the
spring-aop
package. One of them is the aop specification interface, Rod Johnson just integrates it, and has no intention to rewrite or modify the specification. Except for a
Advice
top-level interface and a
AdiceException
, the rest mainly end with
Interceptor
spring aop. Some of the notification interfaces inherit the Advice interface but redefine it themselves, for example:
org.springframework.aop.BeforeAdvice
while some adopt the aop specification, for example:
org.aopalliance.intercept.MethodInterceptor
.
Update (2018-09-01):
I'll say a little bit more. First of all, not all notifications implement MethodInterceptor
(surround notifications)
pre-notifications: MethodBeforeAdvice
, BeforeAdvice
only inherits Advice
Post notifications: AfterReturningAdvice
, AfterAdvice
It is also a post notification
introduction notification: IntroductionInterceptor
inherits MethodInterceptor
from the above, the naming is still very standard, right? it is mainly suffixed with advice
.
well, Aspctj, weaving is the process of adding enhancements to specific join points of the target class. According to different implementation technologies, AOP has three ways of weaving.
(1) compile-time weaving, which requires a special Java compiler.
(2) Class load time weaving, which requires the use of a special class loader.
(3) dynamic proxy weaving, which adds enhanced ways to generate subclasses for the target class at run time.
Spring uses dynamic proxy weaving, while AspectJ uses compile-time weaving and class-loading weaving.
Spring AOP
has no intention of shutting out Aspectj
, but adopts an eclectic approach, that is, it inherits the expression syntax of Aspectj
to define pointcuts and enhancements (you can simply understand it as notification). Therefore, if you generally use AOP
, in addition to introducing spring-aop.jar
, you will also refer to aspecj.weaver.jar
(or aspect.tools.jar
). The latter includes the former) to introduce syntax-related tools (some annotations and interfaces, such as @ Before
). Generally, there are several ways to enable Aspectj
, but there are mainly annotations and configurations. Of course, the corresponding container is mainly entity classes, while these classes are mainly prefixed with Aspectj
, such as AspectJMethodBeforeAdvice
and AspectJExpressionPointcutAdvisor
(note that Advisor
is not used for < code
/**
*
*/
@Aspect
@Component
public class UserAdvice {
@Before("execution(* *UserByName(..))")
public void before() {
System.err.println("before...");
}
}
/**
* xml
*/
<aop:config>
<aop:aspect ref="**">
<aop:before pointcut="execution(* *UserByName(..))" method="**">
</aop:aspect>
</aop:config>
``
both of these are not notifications
the classes ending in Interceptor are mainly the implementation classes of the org.aopalliance.intercept.MethodInterceptor
interface, which can be seen in the MethodBeforeAdviceInterceptor
source code:
strengthen AOP