AOP切面应用(1)

172 阅读1分钟

1.基础框架

添加maven依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
</dependency>

controller

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @GetMapping
    @MetricTime(value = "请求耗时")
    public String getUserInfo(String userId, String keyword){
        log.info("查询用户ID...");
        return "OK";
    }
}

测试代码

@Test
public void testController(){
    RestTemplate restTemplate = new RestTemplate();
    String forObject = restTemplate.getForObject("http://localhost:8080/user?userId=988988&keyword=hua.li", String.class);
    log.info(forObject);
}

2.简单应用

2.1打印请求参数

@Slf4j
@Aspect
@Component
public class RequestAspect {
    @Before("execution(public * com.example.aspect.controller.UserController.*(..))")
    public void doAccessCheck(JoinPoint joinPoint) {
        try{
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            String fullName = String.join("#", methodSignature.getDeclaringTypeName(), methodSignature.getName());
            log.info("[request start] api request {} and param is {}", fullName, new ObjectMapper().writeValueAsString(buildParamMap(joinPoint)));
        }catch (Exception e){
            log.warn("parse failed", e);
        }
    }
    private Map<String, Object> buildParamMap(JoinPoint joinPoint) {
        Map<String, Object> ans = new LinkedHashMap<>();
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        if(Objects.nonNull(methodSignature.getDeclaringTypeName())){
            String[] names = methodSignature.getParameterNames();
            Object[] values = joinPoint.getArgs();
            for (int i = 0; i < names.length; i++) {
                ans.put(names[i], values[i]);
            }
        }
        return ans;
    }
}

2.2统计接口耗时

定义注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MetricTime {
    String value() default "";
}

实现切面

@Aspect
@Component
public class MetricAspect {
    @Around("@annotation(metricTime)")
    public Object metric(ProceedingJoinPoint joinPoint, MetricTime metricTime) throws Throwable {
        String name = metricTime.value();
        long start = System.currentTimeMillis();
        try {
            return joinPoint.proceed();
        } finally {
            long t = System.currentTimeMillis() - start;
            // 写入日志或发送至JMX:
            log.info("[Metrics] " + name + ": " + t + "ms");
        }
    }
}