本文共 2893 字,大约阅读时间需要 9 分钟。
项目中我们经常会用到aop切面,比如日志记录;这里简单记录一下springboot是如何使用aop
spring对aop的配置,来自springboot参考手册,Common application properties:
# AOPspring.aop.auto=true # Add @EnableAspectJAutoProxy.spring.aop.proxy-target-class=true # Whether subclass-based (CGLIB) proxies are to be created (true), as opposed to standard Java interface-based proxies (false).
org.springframework.boot spring-boot-starter-aop org.slf4j slf4j-api org.slf4j slf4j-log4j12
/** * Aspect 切面 * 日志切面 */@Aspect@Componentpublic class LogAspect { /** * slf4j日志 */ private final static Logger logger = LoggerFactory.getLogger(LogAspect.class); /** * Pointcut 切入点 * 匹配cn.controller包下面的所有方法 */ @Pointcut("execution(public * cn.controller.*.*(..))") public void webLog(){} /** * 环绕通知 */ @Around(value = "webLog()") public Object arround(ProceedingJoinPoint pjp) { try { logger.info("1、Around:方法环绕开始....."); Object o = pjp.proceed(); logger.info("3、Around:方法环绕结束,结果是 :" + o); return o; } catch (Throwable e) { logger.error(pjp.getSignature() + " 出现异常: ", e); return Result.of(null, false, "出现异常:" + e.getMessage()); } } /** * 方法执行前 */ @Before(value = "webLog()") public void before(JoinPoint joinPoint){ logger.info("2、Before:方法执行开始..."); // 接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); assert attributes != null; HttpServletRequest request = attributes.getRequest(); // 记录下请求内容 logger.info("URL : " + request.getRequestURL().toString()); logger.info("HTTP_METHOD : " + request.getMethod()); logger.info("IP : " + request.getRemoteAddr()); logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs())); } /** * 方法执行结束,不管是抛出异常或者正常退出都会执行 */ @After(value = "webLog()") public void after(JoinPoint joinPoint){ logger.info("4、After:方法最后执行....."); } /** * 方法执行结束,增强处理 */ @AfterReturning(returning = "ret", pointcut = "webLog()") public void doAfterReturning(Object ret){ // 处理完请求,返回内容 logger.info("5、AfterReturning:方法的返回值 : " + ret); } /** * 后置异常通知 */ @AfterThrowing(value = "webLog()") public void throwss(JoinPoint joinPoint){ logger.error("AfterThrowing:方法异常时执行....."); }}
访问获取所有用户接口
代码已经开源、托管到我的GitHub、码云:
GitHub:
码云:
转载地址:http://yqpez.baihongyu.com/