Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java joinPoint中的getAnnotations方法未列出该方法上的注释之一_Java_Spring_Java 7_Aop_Aspectj - Fatal编程技术网

Java joinPoint中的getAnnotations方法未列出该方法上的注释之一

Java joinPoint中的getAnnotations方法未列出该方法上的注释之一,java,spring,java-7,aop,aspectj,Java,Spring,Java 7,Aop,Aspectj,我在春季与AOP合作: 我写了一篇注释 @Retention(RetentionPolicy.RUNTIME) public @interface TestAnnotation { } 我将其用于控制器方法: @ResponseBody @TestAnnotation @RequestMapping(method = RequestMethod.PUT, value = "/path/{variable}") public return_type controller_call(@PathV

我在春季与AOP合作:

我写了一篇注释

@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {

}
我将其用于控制器方法:

@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();
MethodSignature=(MethodSignature)joinPoint.getSignature();
String methodName=signature.getMethod().getName();
类[]parameterTypes=signature.getMethod().getParameterTypes();
Annotation[]annotations=joinPoint.getTarget().getClass().getMethod(方法名,参数类型).getAnnotations();
这列出了RequestMapping和ResponseBody注释,但没有列出我的TestAnnotation


知道为什么吗???

对我来说,这很有效,也许你做错了什么。可能您的示例代码并不能真正反映您的情况。我在一个普通的Java+AspectJ设置中复制了这种情况,只是将SpringLib放在类路径上,而不是使用SpringAOP运行。不过,SpringAOP的结果应该是相同的,因为切入点匹配与本机AspectJ中的情况类似

示例注释:

package de.scrum\u master.app;
导入java.lang.annotation.Retention;
导入java.lang.annotation.RetentionPolicy;
@保留(RetentionPolicy.RUNTIME)
public@interface TestAnnotation{}
具有入口点的示例类:

package de.scrum\u master.app;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.ResponseBody;
公共类应用程序{
@应答器
@遗嘱说明
@RequestMapping(method=RequestMethod.PUT,value=“/path/{variable}”)
公共字符串控制器\u调用(@PathVariable字符串变量){
返回“虚拟值”;
}
公共静态void main(字符串[]args){
新应用程序().controller_调用(“my/path”);
}
}
带有示例切入点/建议的方面:

package de.scrum\u master.aspect;
导入java.lang.annotation.annotation;
导入org.aspectj.lang.JoinPoint;
导入org.aspectj.lang.annotation.Aspect;
导入org.aspectj.lang.annotation.Before;
导入org.aspectj.lang.reflect.MethodSignature;
@面貌
公共类MyAspect{
@在(“执行(!static**..Application.*(..”)之前)
public void myAdvice(JoinPoint JoinPoint)抛出可丢弃的{
系统输出打印LN(连接点);
MethodSignature=(MethodSignature)joinPoint.getSignature();
String methodName=signature.getMethod().getName();
类[]parameterTypes=signature.getMethod().getParameterTypes();
Annotation[]annotations=joinPoint.getTarget().getClass().getMethod(方法名,参数类型).getAnnotations();
用于(注释:注释)
System.out.println(注释);
}
}
控制台输出:

执行(String de.scrum\u master.app.Application.controller\u调用(String))
@org.springframework.web.bind.annotation.ResponseBy()
@de.scrum_master.app.TestAnnotation()
@org.springframework.web.bind.annotation.RequestMapping(headers=[],name=,value=[/path/{variable}],products=[],method=[PUT],params=[],consumes=[]))

我也遇到了同样的问题,解决方案是设置确保设置了运行时保留策略,并且目标类型是方法

@Target(ElementType.METHOD)    
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation { }

请提供一个完全可复制的示例,并尽可能简短。