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

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

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中的注解监测技术。

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
1月前
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
162 26
|
2月前
|
缓存 Java 数据库
SpringBoot缓存注解使用
Spring Boot 提供了一套方便的缓存注解,用于简化缓存管理。通过 `@Cacheable`、`@CachePut`、`@CacheEvict` 和 `@Caching` 等注解,开发者可以轻松地实现方法级别的缓存操作,从而提升应用的性能和响应速度。合理使用这些注解可以大大减少数据库的访问频率,优化系统性能。
214 89
|
5天前
|
JSON 前端开发 Java
Spring MVC常用的注解
@RequestMapping:用于处理请求 url 映射的注解,可用于类或方法上。用于类上,则表示类中 的所有响应请求的方法都是以该地址作为父路径。 @RequestBody:注解实现接收http请求的json数据,将json转换为java对象。 @ResponseBody:注解实现将conreoller方法返回对象转化为json对象响应给客户。 @Controller:控制器的注解,表示是表现层,不能用用别的注解代替 @RestController : 组合注解 @Conntroller + @ResponseBody @GetMapping , @PostMapping , @Put
|
5天前
|
Java Spring
Spring Boot的核心注解是哪个?他由哪几个注解组成的?
Spring Boot的核心注解是@SpringBootApplication , 他由几个注解组成 : ● @SpringBootConfiguration: 组合了- @Configuration注解,实现配置文件的功能; ● @EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项 ● @ComponentScan:Spring组件扫描
|
7天前
|
Java 测试技术 Spring
SpringBoot+@Async注解一起用,速度提升
本文介绍了异步调用在高并发Web应用性能优化中的重要性,对比了同步与异步调用的区别。同步调用按顺序执行,每一步需等待上一步完成;而异步调用无需等待,可提升效率。通过Spring Boot示例,使用@Async注解实现异步任务,并借助Future对象处理异步回调,有效减少程序运行时间。
|
3月前
|
Java Spring
【Spring】方法注解@Bean,配置类扫描路径
@Bean方法注解,如何在同一个类下面定义多个Bean对象,配置扫描路径
209 73
|
4天前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
17 0
|
3月前
|
Java Spring 容器
【SpringFramework】Spring IoC-基于注解的实现
本文主要记录基于Spring注解实现IoC容器和DI相关知识。
72 21
|
3月前
|
存储 Java Spring
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
101 12
|
3月前
|
Java Spring
【Spring配置】idea编码格式导致注解汉字无法保存
问题一:对于同一个项目,我们在使用idea的过程中,使用汉字注解完后,再打开该项目,汉字变成乱码问题二:本来a项目中,汉字注解调试好了,没有乱码了,但是创建出来的新的项目,写的注解又成乱码了。

热门文章

最新文章