spring boot使用AOP
1.在pom文件中添加依赖:
<!--spring boot aop切面-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2.建立如下所示类:WebLogAspect
3.WebLogAspect类如下:
package com.example.mapper.mybatisMap.aspect; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest;
import java.util.Arrays; @Aspect
@Component
public class WebLogAspect {
private Logger logger = LoggerFactory.getLogger(getClass()); ThreadLocal<Long> startTime = new ThreadLocal<>(); /*execution(): 表达式的主体
*第一个“*”符号: 表示返回值的类型任意
* com.example.mapper.mybatisMap.controller: AOP所切的服务的包名,即,需要进行横切的业务类
* 包名后面的“..”: 表示当前包及子包
* 第二个“*”: 表示类名,* 即所有类
* .*(..); 表示任何方法名,括号表示参数,两个点表示任何参数类型
* */
@Pointcut("execution(public * com.example.mapper.mybatisMap.controller..*.*(..))")
public void webLog(){} @Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
startTime.set(System.currentTimeMillis());
//接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
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()));
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
//处理完请求,返回内容
logger.info("RESPONSE : " + ret);
logger.info("SPEND TIME : " + (System.currentTimeMillis() - startTime.get()));
}
}
4.在com.example.mapper.mybatisMap在建立一个包controller,在controller建立一个HelloController类:
@RestController
public class HelloController { private Logger logger = LoggerFactory.getLogger(getClass()); /**
* hello world 第一个程序
* @return
*/
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}
5.运行程序,访问http://localhost:8080/hello ,控制台输出:
浏览器页面输出:
Hello World!