简体   繁体   English

使用特定注释对所有方法调用带注释的方面

[英]Invoke annotated aspect on all methods with a specific annotation

I have an application based on MVC and I want to log inputs/outputs from all my controllers.我有一个基于 MVC 的应用程序,我想记录所有控制器的输入/输出。
Also, each of my controller methods is annotated with some specific annotation (eg: @MyControllerMethod ).此外,我的每个 controller 方法都带有一些特定的注释(例如: @MyControllerMethod )。
I wrote an Annotated Aspect which works fine when I use the annotation (eg: LogRequestReply ) from the aspect on my controller methods.我写了一个带注释的方面,当我在我的 controller 方法的方面使用注释(例如: LogRequestReply )时,它可以正常工作。

@Around("@annotation(com.xyz.LogRequestReply)")
public Object logRequestResponse(ProceedingJoinPoint pjp) throws Throwable {
@MyControllerMethod
@LogRequestReply /* It invokes my logRequestResponse method */
public ResponseEntity controllerMethodA(...) {}

However, I have a lot of controller methods and each with annotation MyControllerMethod .但是,我有很多 controller 方法,每个方法都带有注释MyControllerMethod
Is there a way I can make my aspect annotation make work on all controller methods by default, such that each of the methods automatically calls my aspect logger and I can log the inputs/outputs?有没有一种方法可以让我的方面注释默认在所有 controller 方法上工作,这样每个方法都会自动调用我的方面记录器并且我可以记录输入/输出?

Note: I also looked into writing an interceptor extending from HandlerInterceptorAdapter .注意:我还考虑编写一个从HandlerInterceptorAdapter扩展的拦截器。 But it has its own complexity of being able to log request/response where the stream can only be read once.但它有其自身的复杂性,即能够记录请求/响应,其中 stream 只能读取一次。

Please suggest.请建议。

One option is to let the @Around definition less restrictive, add a restriction by package or something like that.一种选择是让@Around 定义的限制更少,通过 package 或类似的东西添加限制。 Internally you can check if this is a Controller by this check bellow and then print the request/response:在内部,您可以通过以下检查检查这是否是 Controller,然后打印请求/响应:

@Pointcut("within(com.mypackage.controller..*)")
private void inControllerPackage() {
}

@Around("inControllerPackage()")
public Object logRequestResponse(ProceedingJoinPoint pjp) throws Throwable {
    if (pjp.getSignature().getDeclaringType().isAnnotationPresent(RestController.class)) {
       ...
    } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用反射在“src/test”路径中列出所有用特定注释注释的方法? - How to list all methods annotated with specific annotation in "src/test" path using Reflections? 用注解注解的方法的方面,用其他注解注解 - Aspect for method annotated with annotation which are annotated with another annotation 具有注释@Transactional(readOnly = false)的所有服务方法周围的切入点或切面 - Pointcut or Aspect Around All Service Methods with Annotation @Transactional(readOnly = false) 反映在特定类中获取所有方法的批注 - Reflections getting all methods within a specific class which are annotated @AspectJ 切入点用于具有特定注释的类的所有方法 - @AspectJ pointcut for all methods of a class with specific annotation 如何排除对使用特定注释注释但未使用其他注释注释的bean的扫描? - How to exclude scanning of beans that annotated with specific annotation and not annotated with another annotation? 如何在方面内调用钩子类的方法? - How to invoke methods of hooked class within aspect? 如何获取使用特定注释注释的对象的所有字段和属性? - How do I get all fields and properties of an object that are annotated with specific annotation? 如何通过反射找到用特定注释注释的字段? - how to find field that annotated with specific annotation by reflection? 使用 AspectJ(无 Spring)仅在带注释的方法上应用方面 - Apply aspect only on annotated methods using AspectJ(without Spring)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM