在ASPECT中的方法中访问业务方法的局部变量 [英] Access a business method's local variable in a method which is in an ASPECT

查看:230
本文介绍了在ASPECT中的方法中访问业务方法的局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从方面类中的方法中的业务类中的方法访问局部变量.例如

I want to access a local variable from a method in a business class, in a method which is in an aspect class. For instance

class BusinessClass {
    public void simpleTest() {
        ...
        String localString = new String( "test" );
        ...
    }
} 

MyAspect {
    log() {
        // I WANT TO ACCESS THE VALUE OF LOCALSTRING HERE
    }
}

我想在MyAspect的log方法中访问localString的值.请让我知道是否有任何方法可以使用Spring/AspectJ完成此操作.另外,有没有一种方法可以在不更改simpleTest方法签名的情况下完成?

I want to access localString's value in log method of MyAspect. Please let me know if there is any way to accomplish this using Spring / AspectJ. Also, is there is a way to accomplish without changing simpleTest method signature?

非常感谢!

推荐答案

据我了解,方面旨在应用于许多方法(由切入点定义).这样,他们看不到方法的内部:只是方法的参数和方法的结果.这意味着您不能直接 完成所需的操作,但是您可以尝试将您的方法重构为两部分,一个将localString作为参数,另一个应用默认值做到这一点.这样可以为您提供一个方便的附加连接点. ( AspectJ参考列表连接点,以及对局部变量不是其中之一.)如果将内部"方法设为私有或程序包私有,则您甚至都不会更改该方法通常理解的签名(因为外部代码将无法依赖于该变量).引入的方法).

As I understand them, aspects are intended to be applicable to many methods (as defined by the pointcut). As such, they don't see the internals of the method: just the arguments to the method and the result from it. This means that what you want can't be done directly, but you could try refactoring your method into two pieces, one that takes the localString as an argument, and the other which applies a default value to it. That will then give you a nice convenient joint point to attach to. (The AspectJ reference lists join points, and references to local variables aren't one of them.) If you make the "inner" method private or package-private, you won't even change the generally-understood signature of the method (since external code won't be able to depend on the introduced method).

这篇关于在ASPECT中的方法中访问业务方法的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆