自定义注解不能拦截controller层

本文介绍如何在SpringMVC框架中配置AOP(面向切面编程),通过在配置文件servlet.xml中加入相关设置,使框架能够支持@AspectJ注解并使用cglib生成代理方法。

1,首先在springMVC的配置文件中,webapp/WEB-INF/servlet.xml,加上AOP的相关内容:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!--启动对@AspectJ注解的支持-->
<!--通知spring使用cglib而不是jdk的来生成代理方法 AOP可以拦截到Controller -->
<aop:aspectj-autoproxy proxy-target-class="true"/>

2,可能spring的版本太低了,升级就好。

转载于:https://www.cnblogs.com/esther-qing/p/7755561.html

在软件开发中,尤其是基于Spring框架的应用程序里,通过为Controller添加自定义注解来记录操作日志是一种常见的需求。这种技术可以帮助开发者跟踪用户的请求、分析系统性能以及排查潜在问题。 ### 实现步骤 1. **创建自定义注解** 首先需要定义一个新的注解用于标记哪些方法的操作应该被记录下来。 ```java @Retention(RetentionPolicy.RUNTIME) // 注解会在运行时保留 @Target(ElementType.METHOD) // 表明该注解只能应用于方法上 public @interface LogOperation { String value() default ""; // 可以在这里设置一些额外的信息字段 } ``` 2. **编写AOP切面处理逻辑** 使用Spring AOP(面向方面编程),我们可以捕获所有带有我们刚刚创建的`LogOperation`注解方法,并在此基础上实现具体的日志记录功能。 ```java @Component @Aspect public class LogAspect { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Around("@annotation(logOperation)") public Object logMethod(ProceedingJoinPoint joinPoint, LogOperation logOperation) throws Throwable { long startTime = System.currentTimeMillis(); try { Object result = joinPoint.proceed(); // 执行目标方法 recordSuccessLog(joinPoint, logOperation, startTime); return result; } catch (Throwable e){ recordFailureLog(joinPoint, logOperation, startTime, e); throw e; // 如果希望继续抛出异常给外,则可以加上这一句 } } private void recordSuccessLog(ProceedingJoinPoint joinPoint, LogOperation logOperation, Long startTime){ Signature signature = joinPoint.getSignature(); logger.info("成功执行了 {} 方法 - 描述: {}, 耗时: {} 毫秒", signature.getName(), logOperation.value(), System.currentTimeMillis()-startTime ); } private void recordFailureLog(ProceedingJoinPoint joinPoint, LogOperation logOperation, Long startTime, Throwable e){ Signature signature = joinPoint.getSignature(); logger.error("失败执行了 {} 方法 - 描述: {}, 耗时: {} 毫秒", signature.getName(), logOperation.value(), System.currentTimeMillis()-startTime ,e); } } ``` 此代码片段会拦截每一个加有 `@LogOperation` 的方法并自动打印相应的信息到控制台或者文件中去。 3. **使用示例** 接下来就是在实际项目里面应用它了: ```java @RestController @RequestMapping("/example") public class ExampleController { @GetMapping("/{id}") @LogOperation("获取用户详情") // 自定义描述文字 public ResponseEntity<User> getUserById(@PathVariable Integer id){ User user = userService.findById(id); if(user == null) return new ResponseEntity<>(HttpStatus.NOT_FOUND); return new ResponseEntity<>(user, HttpStatus.OK); } } ``` 当访问上述API路径 `/example/{someId}` 时,“获取用户详情”将会作为一条新纪录保存至指定位置供后续审查之用。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值