- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 Spring 使用 AOP:
我写了一个注释
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
}
我在 Controller 方法上使用它:
@ResponseBody
@TestAnnotation
@RequestMapping(method = RequestMethod.PUT, value = "/path/{variable}")
public return_type controller_call(@PathVariable String variable) {
return service.methodName(variable);
}
在建议中我编写了以下代码:
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature.getMethod().getName();
Class<?>[] parameterTypes = signature.getMethod().getParameterTypes();
Annotation[] annotations = joinPoint.getTarget().getClass().getMethod(methodName, parameterTypes).getAnnotations();
这列出了 RequestMapping 和 ResponseBody 注释,但没有列出我的 TestAnnotation。
知道为什么吗?
最佳答案
对我来说这有效,也许你做错了什么。可能您的示例代码并不能真正反射(reflect)您的情况。我在普通的 Java + AspectJ 设置中复制了这种情况,只是将 Spring 库放在类路径上,但不与 Spring AOP 一起运行。不过,与 Spring AOP 的结果应该是相同的,因为切入点匹配就像在原生 AspectJ 中一样。
示例注释:
package de.scrum_master.app;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {}
带有入口点的示例类:
package de.scrum_master.app;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
public class Application {
@ResponseBody
@TestAnnotation
@RequestMapping(method = RequestMethod.PUT, value = "/path/{variable}")
public String controller_call(@PathVariable String variable) {
return "dummy value";
}
public static void main(String[] args) {
new Application().controller_call("my/path");
}
}
带有示例切入点/建议的方面:
package de.scrum_master.aspect;
import java.lang.annotation.Annotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
@Aspect
public class MyAspect {
@Before("execution(!static * *..Application.*(..))")
public void myAdvice(JoinPoint joinPoint) throws Throwable {
System.out.println(joinPoint);
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature.getMethod().getName();
Class<?>[] parameterTypes = signature.getMethod().getParameterTypes();
Annotation[] annotations = joinPoint.getTarget().getClass().getMethod(methodName, parameterTypes).getAnnotations();
for (Annotation annotation : annotations)
System.out.println(annotation);
}
}
控制台输出:
execution(String de.scrum_master.app.Application.controller_call(String))
@org.springframework.web.bind.annotation.ResponseBody()
@de.scrum_master.app.TestAnnotation()
@org.springframework.web.bind.annotation.RequestMapping(headers=[], name=, value=[/path/{variable}], produces=[], method=[PUT], params=[], consumes=[])
关于java - joinPoint 中的 getAnnotations 方法未列出该方法的注释之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28351440/
这是我第一次接触 AOP。我有一个带有一个方面的 spring-boot 应用程序,一个记录器。搜索我得出的结论是 @Around 方法在方法之前和之后执行(我只在一种方法中调用它),这是对的吗? 在
假设我有这个方法: public void exampleMethod(String myString, Integer myInteger, Object myObject){} 如果我有一个方面
我正在使用 Java Spring Mvc 和 Spring AOP 从用户那里查找参数名称。 我有一个 Controller ,它从用户那里获取参数并调用服务。 我有一个方面在服务之前运行。 该方面
AspectJ的JoinPoint是一个接口(interface),ProceedingJoinPoint也是一个扩展Joinpoint的接口(interface)。 但是,当我在某个方面使用它们时,
我在 Spring 使用 AOP: 我写了一个注释 @Retention(RetentionPolicy.RUNTIME) public @interface TestAnnotation { } 我
我正在学习面向方面的编程概念和 Spring AOP。我无法理解切入点和连接点之间的区别——它们对我来说似乎都是一样的。切入点是您应用建议的地方,而连接点也是我们可以应用建议的地方。那有什么区别呢?
我需要从某个方面的 joinPoint 获取像 POST/PATCH/GET/etc 这样的 http 方法。 @Before("isRestController()") public voi
我有以下建议代码: @Around("annotatedMethod()") public Object aroundGetPanel(ProceedingJoinPoint join
LoggingAspect.java @Around("allGenericAppServiceImplMethods()") public Object LoggingAdvice(Proceedi
我有一个应用程序(使用注释的 Spring 4 MVC+Hibernate 4+MySQL+Maven 集成示例),使用基于注释的配置将 Spring 与 Hibernate 集成。如果可能的话,我想
我正在使用 maven 来构建我的项目。之前编译成功。当我执行 mvn clean install 时,它开始出现错误。 [ERROR] classpath error: unable to find
(标题不是最好的,但我找不到一个好的方式来表达以下问题) 给定 @Aspect @Component class MyAspect { @Autowired private MyService s
我想检索调用特定方法的调用方法。 示例: 我考虑的方法: public void methodA(int a, int b){...} 在测试方法和程序本身中调用 @Test public void
我正在处理Spring AOP,我的代码是这样的 - @Component public class MyClass implements MethodInterceptor{ @Overri
我有一个带有 @Around 注释的简单方面(见下文)。当应用程序不使用响应式范例时,此方面有效。但是当应用程序返回 Mono 或 Flux 时就无法正常工作。 我需要获取从方法返回的对象来生成 JS
同时使用@Around 方面和Spring 引导。在 joinPoint 执行之前创建变量、在 joinPoint 执行期间使其可用以收集其中的数据以及在 joinPoint 执行之后使用变量中收集的
我是一名优秀的程序员,十分优秀!