Components of AOP
■ crosscutting concerns: which methods are intercepted and how to deal with them after interception. These concerns are called crosscutting concerns
■ aspect: class is the abstraction of object features, and aspect is the abstraction of crosscutting concerns
■ join point: the intercepted method, field or construction method
■ pointcut: the definition of intercepting connection points
■ advice: code to be executed after intercepting the connection point
■ target: the target object of the delegate
AOP defining pointcuts
<aop:pointcut expression="execution(public void com.springdemo.service.impl.ProductServiceImpl.work(..) )" id="p1"/>
AOP definition section method
<aop:aspect ref="myAdvice"></aop:aspect>
AOP enhancement types
advance, notification, called enhancement in spring, may be more reasonable. The so-called enhancement is actually to inject some code into each program to enhance the function of the original program. Spring uses enhanced classes to define crosscutting logic. At the same time, since spring only supports method connection points, the enhancement also includes the location information of crosscutting code at which point of the method, so the enhancement includes both crosscutting logic and some connection points.
-
Pre enhanced aop:before doBefore(JoinPoint jp) the first four binding functions are in this form
-
Post enhanced aop:after
-
Abnormal enhanced AOP: after returning
-
Return enhanced AOP: after throwing
-
Surround enhancement AOP: surround doaround (proceeding joinpoint JP) surround enhancement takes this form
Enhanced name Interface describe Pre enhancement org.springframework.aop.BeforeAdvice Enhancements are implemented before the target method is executed. Post enhancement org.springframework.aop.AfterReturningAdvice Enhancements are implemented after the target method is executed. Surround enhancement org.aopalliance.intercept.MethodInterceptor Enhancements are implemented before and after the implementation of the target method. Surround enhancement is an interface defined by the AOP alliance, and the other four enhanced interfaces are Spring defined interfaces. Exception throw enhancement org.springframework.aop.ThrowsAdvice Implement enhancements after the target method throws an exception Return enhancement org.springframework.aop.Introductioninterceptor Represents adding some new methods and properties to the target class. Introduction enhancement is a special enhancement. He can add attributes and methods to the target class, define an interface through interception, and let the target agent implement this interface. Its join points are class level, while the previous ones are method level.
AOP enhanced category operation sequence
The execution sequence is pre enhancement ----- > objective function --------- > post enhancement --------- > return enhancement and exception enhancement
The return enhancement indicates that the program has no exception or error, that is, the exception enhancement does not execute or does not exist. When the exception enhancement exception is interrupted by the catch program, there will be no return, that is, the return enhancement does not exist or does not execute
AOP enhanced method binding mode
<aop:before method="doBefore" pointcut-ref="p1"></aop:before> <aop:after method="doAfter" pointcut-ref="p1"></aop:after> <aop:after-returning method="doReturn" pointcut-ref="p2"></aop:after-returning> <aop:after-throwing method="doException" pointcut-ref="p1"></aop:after-throwing> <aop:around method="doAround" pointcut-ref="p1"></aop:around>
Use of open @ annotation in AOP and scanning switch package
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
Use of AOP annotation form
Return enhancement shall be marked with returning exception enhancement shall be marked with throwing
@AfterReturning(value="execution(public int com.springdemo.service.impl.OtherServiceImpl.*(..))",returning = "re")
@AfterThrowing(value="execution(public int mul(int,int))",throwing="e")
The rest are
@Around("execution(public int *(..))")
@After("execution(public int mul(..))")
@Before("execution(public int mul(int,int))")