如何獲得註釋作爲參數爲 級別註釋定義的參數傳遞?可能嗎?@AspectJ類級別註釋建議與註釋作爲方法參數
從帖子here我能夠得到標識所有公共方法的標記由特定註釋標記的切點。我也能夠得到建議。但是,我不知道如何在上面的情況下獲取作爲參數傳遞的註釋變量。
對於方法級別的註釋,我能夠得到切入點和建議,我可以將註釋作爲參數傳遞,但我不知道如何爲類級註釋實現相同。
下面的代碼有效,但我需要在下面的程序中獲取註釋作爲參數「LogExecutionTimeByClass」的參數,我無法獲得相應的建議或切入點。
譯註:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {
String level();
}
看點:
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LogTimeAspect {
/*
* Pointcut to match all the public methods.
*/
@Pointcut("execution(public * *(..))")
public void publicMethod() {}
/*
* Advice for the public methods that are marked with Annotation "LogExecutionTime" and it works as expected no issue.
*/
@Around("publicMethod() && @annotation(annotation) ")
public Object LogExecutionTimeByMethod(final ProceedingJoinPoint joinPoint,final LogExecutionTime annotation) throws Throwable
{
System.out.println("Invoking the method " +joinPoint.getSignature() +" by LogExecutionTimeByMethod Advice");
return joinPoint.proceed();
}
/*
* Pointcut to match all the public methods that are defined under the Class marked with Annotation LogExecutionTime.
*/
@Pointcut("within(@LogExecutionTime *)")
public void beanAnnotatedWithMonitor() {}
@Pointcut("publicMethod() && beanAnnotatedWithMonitor()")
public void publicMethodInsideAClassMarkedWithAtMonitor() {}
/*
* Below Advice works but I need the LogExecutionTime annotation as an argument to below method. (similar to the advice "LogExecutionTimeByMethod"
* defined above)
*/
@Around("publicMethodInsideAClassMarkedWithAtMonitor()")
public Object LogExecutionTimeByClass(final ProceedingJoinPoint joinPoint) throws Throwable
{
System.out.println("Invoking the method " +joinPoint.getSignature() +" by LogExecutionTimeByClass Advice");
//System.out.println("Invoked by " + annotation.value()); //Need the Annotation Variable here as well...
return joinPoint.proceed();
}
/*
*/
}
註解類:
@LogExecutionTime(level="Class_Level_Invocation")
public class Operator {
@LogExecutionTime(level="Method_Level_Invocation")
public void operate() throws InterruptedException {
Thread.sleep(1000);
}
public void operate1() throws InterruptedException {
Thread.sleep(1000);
}
}
主要課程:
public class AspectJMain {
public static void main(String[] args) throws InterruptedException {
Operator op = new Operator();
op.operate();
op.operate1();
}
}
輸出:
Invoking the method void Operator.operate() by LogExecutionTimeByMethod Advice
Invoking the method void Operator.operate() by LogExecutionTimeByClass Advice
Invoking the method void Operator.operate1() by LogExecutionTimeByClass Advice
請注意,使用Spring不是一個選項。我必須使用AspectJ編譯器。 我編譯我的類並將它們打包爲jar,並使用ApsectJ編譯器使用下面的命令編織該方面。
AJC -inpath core.jar添加-outjar .. \ LIB \ core_woven.jar -1.5
任何指針將是有益的。
謝謝。讓我試試。 Around的原因是獲取該方法的執行時間。 – param83
我嘗試過,但無法通過註釋參數通過內部但下面的切點工作@Pointcut(「publicMethod()&& beanAnnotatedWithMonitor()&& @annotation(annotation)」) – param83
您真的不需要'beanAnnotatedWithMonitor()',請參閱我上面的更新。 – kriegaex