sequence
This is the 6th day of my participation in the Gwen Challenge in November.
Start with AOP dependencies
<! -- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop --> <dependency> < the groupId > org. Springframework. Boot < / groupId > < artifactId > spring - the boot - starter - aop < / artifactId > < version > 2.5.2 < / version > </dependency>Copy the code
About AOP suggestion reader learns to understand well ha, here did not introduce, a word: very important very important!!
Create and write relevant code
Custom annotations
/** * custom log annotation ** @author Smile */ @target ({ElementType. Elementtype. TYPE}) @Retention(retentionPolicy.runtime) @documented Public @interface SysLog {/** * Record business name ** @return Service name */ String value() default ""; }Copy the code
What I’m setting up here is method level
Cut class
@Resource private SysLogService logService; private SysLogPO sysLogPO = new SysLogPO(); /** * where to cut in: a concrete class or annotation, or an obscure classpath **.. *Api.*(..) * / the @pointcut (" @ the annotation (com. Smile. SSM. Aop. The annotation. SysLog) ") public void annotationPointcut () {} / prior notice * * *, * * @param joinPoint xx */ @before ("annotationPointcut()") public void beforePointcut(joinPoint joinPoint) { System.out.println("beforePointcut"); } /** * wrap around notification, Execute * * @param point * @return * @throwable */ @around ("annotationPointcut()") public Object Around the method doAround(ProceedingJoinPoint point) throws Throwable { System.out.println("doAround"); RequestAttributes ra = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes sra = (ServletRequestAttributes) ra; HttpServletRequest request = sra.getRequest(); // This is the method type that gets the call :get post... String method1 = request.getMethod(); String url = request.getRequestURL().toString(); sysLogPO.setMethod(url); sysLogPO.setDeleted(false); sysLogPO.setCreateTime(LocalDateTime.now()); String targetName = point.gettarGet ().getClass().getName(); String methodName = point.getSignature().getName(); Object[] arguments = point.getargs (); try { Class targetClass = Class.forName(targetName); [] methods = targetClass.getMethods(); String value = ""; for (Method method : Methods) {if (method.getName().equals(methodName)) {Class[] clazzs = method.getparameterTypes (); if (clazzs.length == arguments.length) { value = method.getAnnotation(SysLog.class).value(); break; } } } sysLogPO.setBusinessName(value); } catch (Exception e) { e.printStackTrace(); } logService.save(sysLogPO); Log.info ("=== request start, parameters, url: "+ URL + ", method: " + method1 + ", uri:" + request.getRequestURI() + ", params:" + request.getQueryString()); if (arguments ! = null) {for (argument: arguments) {log.info("=== "+ json.tojsonString (argument)); } } return point.proceed(); } /** * return notification, * * @afterreturning ("annotationPointcut()") public void doAfterReturning(joinPoint joinPoint) { System.out.println("doAfterReturning"); } /** * exception notification, @afterThrowing ("annotationPointcut()") public void doAfterThrowing(JoinPoint JoinPoint) { System.out.println("doAfterThrowing"); } /** * after notification, */ @after ("annotationPointcut()") public void doAfter(JoinPoint JoinPoint) {system.out.println ("doAfter"); }Copy the code
@pointcut 1) Execution (* *(..) 2) Execution (public * com.smile.ssm.service.*Api*(..) ) / / means to match com. Smile. SSM. All public methods in the UserService 3) execution (* com. Smile. SSM.. *. * (..) ) // matches all methods under the com.Savage.server package and its subpackagesCopy the code
@joinPoint xx */ @before ("annotationPointcut()")Copy the code
/** * execute Around method ** @param point * @return * @throws Throwable */ @around ("annotationPointcut()")Copy the code
/** * return the notification, after the method returns the result of ** @param joinPoint xx */Copy the code
/** * exception notification, after method throws exception */Copy the code
/** * post-notification, execute */ after method executionCopy the code
Implementation of several enhanced annotations step by step
Normal condition:doAround->beforePointcut->doAfterReturning->doAfter
Abnormal conditions:doAround->beforePointcut->doAfterThrowing->doAfter
Special circumstances:
AfterThrowing will not be caught if an error occurs in after. It is only for methods that are cut in, so be careful to avoid multiple logs.
The above section class test code to write a lot of things is to test the process and collocation:
Log options:
- @Before + @After
- @Around
The above two kinds of casual collocation is good!!
Test
Effect: the log store of the call business is saved and the system can print the call log!!
END
Perfect for aop custom business annotations, next: Project introduces server-side Logback log management.
Source code address: gitee.com/smile_lx/ss…
Today to wash teeth, wow good afflictive ah, a mouth blood taste >! -! <