Quantcast
Channel: 小蓝博客
Viewing all articles
Browse latest Browse all 3155

SpringBoot:SpringBoot通过注解监测Controller接口

$
0
0

SpringBoot:通过注解监测Controller接口

在Spring Boot应用中,通过注解监测Controller接口是一种常见且有效的方式,用于记录接口的访问日志、性能监控、异常处理等。本文将详细介绍如何使用Spring Boot注解来监测Controller接口,并提供一个完整的示例。

一、基本概念

1.1 Spring AOP

Spring AOP(面向切面编程)是实现注解监测的重要技术。AOP可以在方法执行的各个阶段(如前后)添加额外的功能,而不需要修改方法本身。常用的AOP术语包括:

  • 切面(Aspect):包含切入点和通知的模块。
  • 切入点(Pointcut):定义在哪些地方应用通知。
  • 通知(Advice):定义具体的横切逻辑,如日志记录。

1.2 自定义注解

自定义注解是标记Controller方法的基础,通过注解可以灵活地应用不同的监控策略。

二、实现步骤

2.1 创建自定义注解

首先,创建一个自定义注解,用于标记需要监测的Controller方法。

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

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

2.2 创建AOP切面

接下来,创建一个AOP切面,拦截带有 @Monitor注解的方法,并添加监测逻辑。

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MonitorAspect {

    @Around("@annotation(monitor)")
    public Object around(ProceedingJoinPoint joinPoint, Monitor monitor) throws Throwable {
        long start = System.currentTimeMillis();

        try {
            // 执行目标方法
            Object result = joinPoint.proceed();
            return result;
        } finally {
            long end = System.currentTimeMillis();
            System.out.println("Method [" + joinPoint.getSignature() + "] executed in " + (end - start) + " ms");
        }
    }
}

2.3 应用注解到Controller

在Controller的方法上应用自定义注解 @Monitor

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class DemoController {

    @Monitor("Example monitoring")
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}

2.4 配置Spring Boot应用

确保Spring Boot应用启用了AOP功能,可以在 application.properties中进行必要配置。

spring.aop.auto=true
spring.aop.proxy-target-class=true

三、示例代码

以下是一个完整的示例代码,包含自定义注解、AOP切面和Controller:

3.1 自定义注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

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

3.2 AOP切面

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MonitorAspect {

    @Around("@annotation(monitor)")
    public Object around(ProceedingJoinPoint joinPoint, Monitor monitor) throws Throwable {
        long start = System.currentTimeMillis();

        try {
            // 执行目标方法
            Object result = joinPoint.proceed();
            return result;
        } finally {
            long end = System.currentTimeMillis();
            System.out.println("Method [" + joinPoint.getSignature() + "] executed in " + (end - start) + " ms");
        }
    }
}

3.3 Controller

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class DemoController {

    @Monitor("Example monitoring")
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}

3.4 Spring Boot 配置

spring.aop.auto=true
spring.aop.proxy-target-class=true

四、扩展应用

4.1 参数和返回值日志

除了记录执行时间,还可以记录方法的输入参数和返回值。

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MonitorAspect {

    @Around("@annotation(monitor)")
    public Object around(ProceedingJoinPoint joinPoint, Monitor monitor) throws Throwable {
        long start = System.currentTimeMillis();

        Object result = null;
        try {
            // 记录方法输入参数
            Object[] args = joinPoint.getArgs();
            System.out.println("Method [" + joinPoint.getSignature() + "] called with arguments: " + Arrays.toString(args));

            // 执行目标方法
            result = joinPoint.proceed();

            // 记录方法返回值
            System.out.println("Method [" + joinPoint.getSignature() + "] returned: " + result);
            return result;
        } finally {
            long end = System.currentTimeMillis();
            System.out.println("Method [" + joinPoint.getSignature() + "] executed in " + (end - start) + " ms");
        }
    }
}

4.2 异常处理

在AOP切面中还可以添加异常处理逻辑,捕获并记录方法执行中的异常。

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MonitorAspect {

    @Around("@annotation(monitor)")
    public Object around(ProceedingJoinPoint joinPoint, Monitor monitor) throws Throwable {
        long start = System.currentTimeMillis();

        Object result = null;
        try {
            // 记录方法输入参数
            Object[] args = joinPoint.getArgs();
            System.out.println("Method [" + joinPoint.getSignature() + "] called with arguments: " + Arrays.toString(args));

            // 执行目标方法
            result = joinPoint.proceed();

            // 记录方法返回值
            System.out.println("Method [" + joinPoint.getSignature() + "] returned: " + result);
            return result;
        } catch (Throwable throwable) {
            // 记录异常
            System.err.println("Method [" + joinPoint.getSignature() + "] threw an exception: " + throwable);
            throw throwable;
        } finally {
            long end = System.currentTimeMillis();
            System.out.println("Method [" + joinPoint.getSignature() + "] executed in " + (end - start) + " ms");
        }
    }
}

五、总结

本文详细介绍了如何通过Spring Boot注解监测Controller接口,包括自定义注解、AOP切面的创建和使用以及具体的示例代码。通过这种方式,可以方便地在Controller方法执行前后添加日志记录、性能监控和异常处理逻辑,而无需修改方法本身的代码。这种方法不仅提高了代码的可维护性,还增强了系统的监控能力。希望本文能帮助您更好地理解和应用Spring Boot中的注解监测技术。


Viewing all articles
Browse latest Browse all 3155

Latest Images

Trending Articles