简体   繁体   English

从AspectJ中的带批注方法获取局部变量值

[英]Get local variable value from annotated method in aspectJ

I am using java custom annotation with aspectJ 我在AspectJ中使用Java 自定义注释

CUSTOM ANNOTATION 定制注释

@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface TrxLogger {

String author() default "Shahid Ghafoor";
String comments() default "I am Fan of JAVA";
}

SERVICE 服务

public class Service {

private String name;

public String getName() {
    return name;
}


public void setName(String name) {
    this.name = name;
}

@TrxLogger
public String sayHello(String fn, String ln) {

    this.name=fn;

     String localVariable="Before System.out.println";

    System.out.println("Welcome!" + fn + " " + ln);

    return "The Transaction return Value";
}

}

ASPECT 方面

@Around(value="pcSample(tl)", argNames="tl")
public Object runMethod(ProceedingJoinPoint pjp, TrxLogger tl) throws Throwable {

 return null;
}

is it possible to get local variable value( String localVariable="Before System.out.println"; ) of sayHello() Method in aspect ? 是否可以获得方面的sayHello()方法的局部变量值( String localVariable="Before System.out.println"; )?

No, a local variable is just that, local. 不,局部变量就是局部变量。 You cannot dig into the method to access it. 您无法深入研究访问它的方法。 The body of a method is not accessible through reflection. 方法的主体无法通过反射访问。

when method sayHello execute that time only lovalVariable have value. 当方法sayHello执行该时间时,只有lovalVariable具有值。 otherwise it's not known by anybody. 否则没人知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM