Use the AOP method in the SpringBoot project, springbootaop
This article describes how to use aop in the SpringBoot project. The details are as follows:
1. Overview
Implementing General logic with AOP technology can greatly simplify programming, such as signature verification and authentication. Spring declarative transactions are also implemented through the AOP technology.
Code reference example project https://github.com/qihaiyan/springcamp/tree/master/spring-aop
Spring's AOP technology has four core concepts:
Pointcut: Specifies the method to intercept, such as execution (* cn. springcamp. springaop. service .*.*(..))
Advice: the action to be executed after the method is intercepted
Aspect: a plane that combines Pointcut and Advice to form a plane.
Join Point: an instance of Pointcut during execution
Weaver: A Framework for Implementing AOP, such as AspectJ or Spring AOP.
2. Cut Point Definition
Commonly used Pointcut definitions include execution and @ annotation. Execution defines that there is no intrusion to the method and is used to implement a more common aspect. @ Annotation can be added as an annotation to a specific method, such as the Transaction annotation of Spring.
The execution cut-point definition should be placed in a public class to centrally manage the cut-point definition.
Example:
public class CommonJoinPointConfig { @Pointcut("execution(* cn.springcamp.springaop.service.*.*(..))") public void serviceLayerExecution() {}}
In this way, you can use CommonJoinPointConfig. serviceLayerExecution () to reference the vertex in a specific Aspect class.
public class BeforeAspect { @Before("CommonJoinPointConfig.serviceLayerExecution()") public void before(JoinPoint joinPoint) { System.out.println(" -------------> Before Aspect "); System.out.println(" -------------> before execution of " + joinPoint); }}
When the cut point needs to be changed, you only need to modify the CommonJoinPointConfig class without modifying each Aspect class.
3. Common Section
Before: runs Advice Before the method is executed. It is often used for signature verification and authentication.
After: After the method is executed, whether the execution is successful or an exception is thrown.
AfterReturning: it is executed only after the method is successfully executed.
AfterThrowing: it is executed only after an exception is thrown by the method execution.
A simple Aspect:
@Aspect@Componentpublic class BeforeAspect { @Before("CommonJoinPointConfig.serviceLayerExecution()") public void before(JoinPoint joinPoint) { System.out.println(" -------------> Before Aspect "); System.out.println(" -------------> before execution of " + joinPoint); }}
4. Custom Annotation
Suppose we want to collect the execution time of a specific method. A more reasonable method is to customize an annotation and add this annotation to the method that needs to collect the execution time.
First define an annotation TrackTime:
@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)public @interface TrackTime { String param() default "";}
Then define an Aspect class to implement annotation behavior:
@Aspect@Componentpublic class TrackTimeAspect { @Around("@annotation(trackTime)") public Object around(ProceedingJoinPoint joinPoint, TrackTime trackTime) throws Throwable { Object result = null; long startTime = System.currentTimeMillis(); result = joinPoint.proceed(); long timeTaken = System.currentTimeMillis() - startTime; System.out.println(" -------------> Time Taken by " + joinPoint + " with param[" + trackTime.param() + "] is " + timeTaken); return result; }}
Use this annotation on a method to collect the execution time of this method:
@TrackTime(param = "myService")public String runFoo() { System.out.println(" -------------> foo"); return "foo";}
Note that the @ TrackTime (param = "myService") annotation can pass parameters.
To allow the annotation to pass parameters, you must specify a parameter String param () default "default Value" when defining the annotation ",
At the same time, in the Aspect class, the corresponding parameters are added to the around method. In the @ Around annotation, the parameter variable name trackTime must also be used, instead of the class name TrackTime.
@Around("@annotation(trackTime)")public Object around(ProceedingJoinPoint joinPoint, TrackTime trackTime)
5. Summary
When running a sample project, the console outputs the following content:
-------------> Before Aspect
-------------> Before execution of execution (String cn. springcamp. springaop. service. MyService. runFoo ())
-------------> Foo
-------------> Time Taken by execution (String cn. springcamp. springaop. service. MyService. runFoo () with param [myService] is 8
-------------> After Aspect
-------------> After execution of execution (String cn. springcamp. springaop. service. MyService. runFoo ())
-------------> AfterReturning Aspect
-------------> Execution (String cn. springcamp. springaop. service. MyService. runFoo () returned with value foo
We can see that the execution sequence of several Aspect types is Before After Around AfterReturning (AfterThrowing)
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.