动态地忽略事务方法
我正在编写一个与 SOAP API 对话的程序。我正在编写的程序涉及金钱,能够在运行时确定会很有用(我的理由是,如果我可以在运行时处理它,我可以轻松地将配置文件放在一起以供编译后使用)我是否想要执行处理金钱的方法。
我有一个所有 API 调用都要经过的 make request 方法,理想情况下我想要这样的东西:
...
@DealsWithMoney
public void myMethodWhichMakesAnAPIRequest() {
...
this.makeRequest(...);
}
public void makeRequest(...) {
if (!this.allowMoneyHandlingMethods) {
//Psuedo code
if (method_that_called_this_method has @DealsWithMoney annotation) {
//ignore
}
}
}
如何编写这个自定义注释?或者有更好的方法来处理这个问题吗?
I'm writing a program which talks to a SOAP API. The program I'm writing deals with money, and it would be useful to be able to determine at run time (my reasoning being that if I can deal with it at run time I can easily put together configuration files for use post-compilation) whether or not I want the methods which deal with money to be executed.
I have a make request method which all API calls go through, ideally I would like something like so:
...
@DealsWithMoney
public void myMethodWhichMakesAnAPIRequest() {
...
this.makeRequest(...);
}
public void makeRequest(...) {
if (!this.allowMoneyHandlingMethods) {
//Psuedo code
if (method_that_called_this_method has @DealsWithMoney annotation) {
//ignore
}
}
}
How can I write this custom annotation? Or is there a better method for handling this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将为您定义注释。
获取堆栈跟踪,从中您应该能够获取调用者的
Method
或Member
引用,并检查您要查找的注释是否在返回的列表。
will define the annotation for you.
gets the stack track from which you should be able to get the
Method
orMember
reference of the caller and doand check to see if the annotation you are looking for is in the returned list.