我有一个可用于注释的方面:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DumpToFile {
}
连接点:
@Aspect
@Component
public class DumpToFileAspect {
@Around("@annotation(DumpToFile)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
...
// I likte to read out a parameter from the annotation...
Object proceed = joinPoint.proceed();
...
return proceed;
}
}
我可以在使用@DumpToFile
的方法上成功使用方面。但是,我想将参数传递给注释,并在我的方面中获取它的值。
例如。 @DumpToFile(fileName="mydump")
。有人可以告诉我该怎么做吗?
答案 0 :(得分:1)
将您的@Around
更改为:
@Aspect
@Component
public class DumpToFileAspect {
@Around("@annotation(dumpToFileAspect)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint, DumpToFile dumpToFileAspect) throws Throwable {
...
// I likte to read out a parameter from the annotation...
String fileName = dumpToFileAspect.getFileName();
Object proceed = joinPoint.proceed();
...
return proceed;
}
}
答案 1 :(得分:1)
您应该能够将注释接口传递给拦截器方法。我还没试过自己。
Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DumpToFile {
String fileName() default "default value";
}
在DumpToFileAspect中-
@Aspect
@Component
public class DumpToFileAspect {
@Around("@annotation(dtf)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint, DumpToFile dtf) throws Throwable {
...
// I likte to read out a parameter from the annotation...
System.out.println(dtf.fileName); // will print "fileName"
Object proceed = joinPoint.proceed();
...
return proceed;
}
}
答案 2 :(得分:0)
您可以使用此:
@Around("@annotation(dumpFile)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint,DumpToFile dumpFile)
@annotation
内部必须是DumpToFile
参数名称。
有关详细信息,请参见documentation