赞
踩
aop 实现 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--引入AOP依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
/**
* url 时间统计注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TakeTimeRecord {
/**
* url 名称
* @return
*/
String value() default "";
}
@Aspect
@Order(1)
@Component
@Slf4j
public class TakeTimeRecordAspect {
/**
* 带有@TakeTime注解的方法
* com.example.executiontimedemo.interfaces.TakeTimeRecord (TakeTimeRecord 注解的包路径)
*/
@Pointcut("@annotation(com.example.executiontimedemo.interfaces.TakeTimeRecord)")
public void pointcut() {
}
@Before("pointcut()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
//接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
request.setAttribute("_startTime", System.currentTimeMillis());
}
@After("pointcut()")
public void doAfterReturning(JoinPoint joinPoint) {
//接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
long startTime = (Long) request.getAttribute("_startTime");
//记录请求的内容
log.info("请求URL: {}", request.getServletPath());
log.info("请求METHOD:{}", request.getMethod());
//处理完请求后
log.info("方法执行时间:{}", (System.currentTimeMillis() - startTime));
}
}
DemoController@RestController
public class DemoController {
@TakeTimeRecord("查询所有信息")
@GetMapping(value = "/test")
public Object findAll() {
return new HashMap<>();
}
//添加监控注解
@TakeTimeRecord("通过id查询数据")
@GetMapping(value = "/findById")
public void findById() {
System.out.println("查询一个数据");
}
}

Interceptor 实现LoginInterceptor@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//设置请求开始时间
request.setAttribute("_startTime", System.currentTimeMillis());
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
long startTime = (Long) request.getAttribute("_startTime");
//记录请求的内容
log.info("Interceptor请求URL: {}", request.getServletPath());
log.info("Interceptor请求METHOD:{}", request.getMethod());
log.info("Interceptor方法执行时间:{}", (System.currentTimeMillis() - startTime));
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
LoginConfig@Configuration
public class LoginConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
/**配置登录拦截器*/
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")
//忽略的url
.excludePathPatterns("/login/**")
;
}
}

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。