對我來說這個工作,也許你做錯了什麼。可能你的示例代碼並不真正反映你的情況。我已經在普通的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=[])
請提供一個完全重複的例子,並使其儘可能短。 – 2015-02-06 16:10:50