首页 \ 问答 \ 使用AspectJ Annotation获取方法输入的属性(Get Method Input's Attributes using AspectJ Annotation)

使用AspectJ Annotation获取方法输入的属性(Get Method Input's Attributes using AspectJ Annotation)

我正在尝试获取方法输入的一些属性,但我只能获得方法的输入,而无需任何选项来访问属性或使用任何get方法。

例如,这是我的代码

@Around("execution (* *(cloudFile))")
public Object captureFileAttribute(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        System.err.println(joinPoint.getArgs()[0]);
        return result;
    }

其中cloudFile基本上是一个包含很多东西的类,包括一个文件,并且可以通过使用cloudFile.getSize()来获取文件的大小; (得到方法)。 基本上,cloudFile类看起来像这样:

public class CloudFile implements Comparable<CloudFile> {
...

public CloudFile(CloudFolder folder, File file, FileSyncStatus syncStatus, TrustLevel trustLevel, String checksum) {
...
}
...
public long getSize() {
        return size.get();
    }
...
}

从上面的代码我只能打印文件的名称,如a.txt但没有选项访问文件中包含的属性(在这种情况下,我希望得到5 KB的大小)。 有没有办法从AspectJ传递的参数中访问变量或方法? 有没有选择呢?

谢谢

UPDATE

我发现类似的东西在正常的AspectJ中回答我的问题是这样的:

public privileged aspect MemberAccessRecipe 
{
   /*
   Specifies calling advice whenever a method
   matching the following rules gets executed:

   Class Name: MyClass
   Method Name: foo
   Method Return Type: void
   Method Parameters: an int followed by a String
   */
   pointcut executionOfFooPointCut( ) : execution(
      void MyClass.foo(int, String));

   // Advice declaration
   after(MyClass myClass) : executionOfFooPointCut( ) && this(myClass)
   {
      System.out.println(
         "------------------- Aspect Advice Logic --------------------");
      System.out.println(
         "Accessing the set(float) member of the MyClass object");
      System.out.println(
         "Privileged access not required for this method call as it is 
         public");
      myClass.setF(2.0f);
      System.out.println(
         "Using the privileged aspect access to the private f member 
         variable");
      System.out.print("The current value of f is: ");
      System.out.println(myClass.f);
      System.out.println(
         "Signature: " + thisJoinPoint.getSignature( ));
      System.out.println(
         "Source Line: " + thisJoinPoint.getSourceLocation( ));
      System.out.println(
         "------------------------------------------------------------");
   }
}

现在我很困惑,并试图在AspectJ注释中找到相同的写作。


I'm trying to get some attributes of method's input but I'm only able to get the method's input only without any option to access the attributes or use any get method.

For example here is my code

@Around("execution (* *(cloudFile))")
public Object captureFileAttribute(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        System.err.println(joinPoint.getArgs()[0]);
        return result;
    }

where cloudFile is basically a class that contains a lot of stuff, including a file and it's possible to get the size of the file by using cloudFile.getSize(); (get method). Basically the cloudFile class looks like this:

public class CloudFile implements Comparable<CloudFile> {
...

public CloudFile(CloudFolder folder, File file, FileSyncStatus syncStatus, TrustLevel trustLevel, String checksum) {
...
}
...
public long getSize() {
        return size.get();
    }
...
}

From the above code I'm only able to print the name of file, like a.txt but without option of accessing the attributes contained in the file (in this case I would like to get the size of 5 KB). Is there a way to access the variables or methods from the arguments passed on by the AspectJ? Is there any option to do that?

Thanks

UPDATE

I have found something similar to answer my question in normal AspectJ something like this:

public privileged aspect MemberAccessRecipe 
{
   /*
   Specifies calling advice whenever a method
   matching the following rules gets executed:

   Class Name: MyClass
   Method Name: foo
   Method Return Type: void
   Method Parameters: an int followed by a String
   */
   pointcut executionOfFooPointCut( ) : execution(
      void MyClass.foo(int, String));

   // Advice declaration
   after(MyClass myClass) : executionOfFooPointCut( ) && this(myClass)
   {
      System.out.println(
         "------------------- Aspect Advice Logic --------------------");
      System.out.println(
         "Accessing the set(float) member of the MyClass object");
      System.out.println(
         "Privileged access not required for this method call as it is 
         public");
      myClass.setF(2.0f);
      System.out.println(
         "Using the privileged aspect access to the private f member 
         variable");
      System.out.print("The current value of f is: ");
      System.out.println(myClass.f);
      System.out.println(
         "Signature: " + thisJoinPoint.getSignature( ));
      System.out.println(
         "Source Line: " + thisJoinPoint.getSourceLocation( ));
      System.out.println(
         "------------------------------------------------------------");
   }
}

and now I am confused and trying to find the equal writing in AspectJ annotation.


原文:https://stackoverflow.com/questions/37075638
更新时间:2023-08-26 16:08

最满意答案

我认为这取决于更多的个人方法。 我个人认为,如果我们确实知道做了什么样的改变,那么根本不需要提出任何疑问。 或者我们可以只查询那个特定的对象,而不是整个列表。 或者,如果编辑成功(我最喜欢),我们的API可以配置为从数据库返回新的更新对象。

但有人可能会争辩说刷新整个清单是一个不好的主意。 它是

  • 对数据库的查询较重
  • 在后端将更多项目转换为JSON
  • 通过网络发送更多数据
  • 在前端解析更多JSON

正如你所看到的那样,这很麻烦,如果你有很多用户,他们可能会开始用查询轰炸数据库,尽管每个人都知道(推测性地有点但是大部分是正确的)数据在当时的样子。 因此,如果API无法配置为返回更新的对象,我建议要么只查询更新的对象,要么根本不查询 - 如果我们知道我们请求后端在数据库中做了哪些更改,并且我们知道后端成功地做到了这一点,然后我们只需在我们的前端应用这些更改就没有任何进一步的考虑


This depends on more personal approach, I think. I personally think that making any queries at all is not necessary, if we certainly know what kind of change has been made. Or we can query for that specific object only, not the entire list. Or our API can be configured in a way that it returns the new, updated object from the database if the edit was successful (that's my favorite).

But one may argue that refreshing the entire list is a bad idea. It is

  • A heavier query for the database
  • More items to convert to JSON in backend
  • More data sent through network
  • More JSON to parse in frontend

As you see, this is cumbersome, and if you have many users, they may start bombarding the database with queries, though each knows (speculatively somewhat, but mostly correctly) how there data does look like at the very moment. So if the API cannot be configured to return the updated object, I suggest either to query only for the updated object, or not querying at all -- if we know what changes we requested the backend to make in the database, and we know the backend succeeded in doing that, then we just apply the changes in our frontend without any further thought

相关问答

更多
  • 我会用这种方法在数据库中建模数据。 员工与一周中的每一天的工作时间之间存在多对多的关系。 在用户界面上,您可以使用日期和多选列表框的复选框设置给定日期的小时数。 I would model the data in the database this way. There's a many to many relationship between the employees and hours for each day of the week. On the UI side, you could use ch ...
  • 您的服务返回缓存的数据。 它不应该。 让它在每次调用时发出http请求,而不仅仅是第一次。 否则,显然,它总是返回相同的陈旧数据。 Your service returns cached data. It shouldn't. Make it make an http request every time it's called, not just the first time. Otherwise, obviously, it always returns the same, stale data.
  • 我认为这取决于更多的个人方法。 我个人认为,如果我们确实知道做了什么样的改变,那么根本不需要提出任何疑问。 或者我们可以只查询那个特定的对象,而不是整个列表。 或者,如果编辑成功(我最喜欢),我们的API可以配置为从数据库返回新的更新对象。 但有人可能会争辩说刷新整个清单是一个不好的主意。 它是 对数据库的查询较重 在后端将更多项目转换为JSON 通过网络发送更多数据 在前端解析更多JSON 正如你所看到的那样,这很麻烦,如果你有很多用户,他们可能会开始用查询轰炸数据库,尽管每个人都知道(推测性地有点但是大 ...
  • 在可能的情况下,您可以使用BindingList或ObservableCollection并在列表更改时接收通知,而不是使用循环或时间间隔来监视列表。 然后,您可以更新事件处理程序中的用户界面,该处理程序已达到BindingList ListChanged事件或ObservableCOllection CollectionChanged事件。 例 这是一个基于ObservableCollection的示例。 ObservableCollection l ...
  • 你的方式是练习标准。 只是谷歌将你的jQuery文件CDN,它可以加速你的网站。 您知道,Google在这个世界上拥有服务器。 我认为它应该将您的js代码放入一个单独的文件中。 因为大多数代码的选择器都是处理HTML类元素。 通常,类元素通常是使用的,因此这个代码可能不仅会执行HTML页面。 所以单独放置然后你可以轻松修改它们。 Your way is the practice standard. Just ues Google to CDN your jQuery file, it can speed u ...
  • 当用户单击“删除”按钮时,我希望从数据库中删除匹配项,然后刷新ListView 要从ListView中删除项,请创建一个方法MatchListViewAdapter ,它从getCount方法中使用的data-source的同一实例中删除项目,如: public void removeItem(int pos){ this.dataContainer.remove(pos); this.notifyDataSetChanged(); } 在MatchDao.delete(matchToDelet ...
  • 好吧,不要每次请求都提取所有百万条记录。 请参阅LIMIT和OFFSET http://dev.mysql.com/doc/refman/5.0/en/select.html 您可能应该使用ORM或库来帮助您进行分页 确保您了解SQL参数化(为了安全起见) 我发现像目录这样的列表最好从搜索服务器(如Solr)到数据库,特别是如果你想要搜索方面。 MySQL确实有文本搜索。 Well, don't pull all million records every request. See LIMIT and OF ...
  • 你的渲染函数读取this.props.records ,但你没有更新道具,你正在更新状态。 请阅读this.state.records 。 render() { const records = this.state.records.map((record) => ... Your render function reads this.props.records, but you're not updating props, you're updating state. Read f ...
  • Angular2材料可能会在今年年底前发布测试版。 同时,一个很好的候选者是ng2-材料 。 primeNG和Vaadin Elements提供了更多UI 元素 Angular2 material would probably publish a beta version by the end of the year. A good candidate for meantime is ng2-material. More UI elements are available from primeNG and ...
  • 如果你在多个页面上使用相同的脚本,那么是的,将它放在一个单独的.js文件中并从每个页面引用它可能是有意义的(无论如何我都这样做,即使只在一个页面上使用JavaScript) 。 是的,如果您控制