一、controller方法加切面,记录操作日志
其他应用:记录方法耗时 ljheee/my-monitor-aop (github.com)
1.pom文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency>
2.注解类
@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface LogAnno { String Type(); }
3.切面类
package com.vince.aop; import com.vince.annotation.LogAnno; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import org.aspectj.lang.annotation.Aspect; import java.lang.reflect.Method; /** * @author duanxiaoqiu * @Date 2019-12-03 09:52 **/ @Slf4j @Component @Aspect public class LogAopAspect { //表示匹配带有自定义注解的方法 @Pointcut("@annotation(com.vince.annotation.LogAnno)") public void pointcut() {} @Around("pointcut()") public Object around(ProceedingJoinPoint point) { Object result =null; long beginTime = System.currentTimeMillis(); try { log.info("我在目标方法之前执行!"); result = point.proceed(); long endTime = System.currentTimeMillis(); MethodSignature methodSignature = (MethodSignature) point.getSignature(); // 获取方法 Method method = methodSignature.getMethod(); // 获取方法上面的注解 LogAnno logAnno = method.getAnnotation(LogAnno.class); // 获取操作描述的属性值 String operateType = logAnno.Type(); log.info("我在目标方法之后执行!"); } catch (Throwable e) { // TODO Auto-generated catch block } return result; } }
4.应用
@LogAnno(Type = "添加用户") @RequestMapping("/") @ResponseBody public String index() { log.info("=======1=======" + userName); return "hello world"; }
二、在controller上加切面
package com.vince.aop; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * @author duanxiaoqiu * @Date 2019-12-03 10:12 **/ @Slf4j @Aspect @Component public class LogAspect { /** * Pointcut定义切点 * public修饰符的 返回值任意 com.vince.controller包下面的任意类的任意方法任意参数 */ @Pointcut("execution(public * com.vince.controller.*.*(..))") public void log(){ } @Around("log()") public Object around(ProceedingJoinPoint joinPoint) { Object result =null; long beginTime = System.currentTimeMillis(); try { log.info("我在目标方法之前执行!"); log.info("class.method: " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); log.info("args: "+joinPoint.getArgs()); result = joinPoint.proceed(); log.info("我在目标方法之后执行!"); } catch (Throwable e) { // TODO Auto-generated catch block } return result; } /*@Before("log()") public void doBefore(JoinPoint joinPoint){ log.info("方法执行前..."); log.info("class.method: " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); log.info("args: "+joinPoint.getArgs()); } @After("log()") public void doAfter(JoinPoint joinPoint){ log.info("方法执行后..."); } @AfterReturning(returning="result", pointcut="log()") public void doAfterReturnint(Object result){ log.info("方法返回值:" + result); }*/ }