java 可能的 AOP 用例是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2197051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:00:01  来源:igfitidea点击:

What are the possible AOP use cases?

javaspringjbossaopuse-case

提问by Ondra ?i?ka

I'd like to make a picture of what are the possible cases for effective involvement of AOP in application design. All I have met so far is:

我想描绘一下在应用程序设计中有效参与 AOP 的可能案例。到目前为止,我所遇到的只有:

  • logging-related
  • security checks
  • transaction management
  • tweaking of a legacy application
  • 日志相关
  • 安全检查
  • 交易管理
  • 调整遗留应用程序

Anything else?

还要别的吗?

(It doesn't have to be necessarily Spring's proxy based AOP - rather JBoss AOP.)

(它不一定是基于 Spring 的代理的 AOP——而是 JBoss AOP。)

(Related question)

相关问题

采纳答案by Kevin

I can give you two examples where we use it:

我可以给你举两个我们使用它的例子:

  • Automatically registering objects in JMX for remote management. If a class is annotated with our @AutoRegisterannotation, we have an aspect that watches for new instantiations of that class and registers them in JMX automatically.

  • Audit logging (the gold standard AOP use case). Its a bit coarse but the general approach is to annotate methods that represent some auditable action. Combined with something like Spring Security, we can get a pretty good idea of:

    • who the user is
    • what method they're invoking
    • what data they're providing
    • what time the method was invoked
    • whether the invocation was successful or not (i.e., if an exception was thrown)
  • 自动在 JMX 中注册对象以进行远程管理。如果一个类用我们的@AutoRegister注解进行注解,我们有一个方面来监视该类的新实例并自动在 JMX 中注册它们。

  • 审计日志(黄金标准 AOP 用例)。它有点粗糙,但一般的方法是注释表示一些可审计操作的方法。结合 Spring Security 之类的东西,我们可以很好地了解:

    • 用户是谁
    • 他们正在调用什么方法
    • 他们提供什么数据
    • 调用方法的时间
    • 调用是否成功(即是否抛出异常)

回答by Pedro Ghilardi

To see the coverage of AOP in terms of applicability I really recommend you to read the book Aspect-Oriented-Software-Development-Use-Cases. This book elaborates use cases of functional and non-functional requirements using AOP. After that you will see that aspects can be used to more requirements than logging, tracing, security, etc.

要了解 AOP 在适用性方面的覆盖范围,我真的建议您阅读Aspect-Oriented-Software-Development-Use-Cases 一书。本书详细阐述了使用 AOP 的功能性和非功能性需求的用例。之后,您将看到方面可以用于比日志记录、跟踪、安全等更多的需求。

回答by Adisesha

Method level caching,if your method is stateless(I mean returns same value when invoked repeatedly with same parameter values). This is more effective in case of DAO methods because it avoids database hit.

方法级缓存,如果您的方法是无状态的(我的意思是使用相同的参数值重复调用时返回相同的值)。这在 DAO 方法的情况下更有效,因为它避免了数据库命中。

回答by ewernli

  • Read/write locks. Instead of replicating the same snippet, I used an aspect to define the methods that needed a read lock or an exclusive lock.
  • 读/写锁。我没有复制相同的片段,而是使用一个方面来定义需要读锁或排他锁的方法。

回答by Adeel Ansari

One effective use of AOP, besides all those you listed, can be validation. Validation of user input, or business objects.

除了您列出的所有这些之外,AOP 的一种有效使用可以是验证。验证用户输入或业务对象。

Related articles you must look at.

相关文章你一定要看。

回答by Michael Wiles

  • Exception Handling: don't need to repeat the horrible list of try ... catch, catch, catch etc - also means the exception handling is guaranteed to be consistent.
  • Performance monitoring: Very useful as using an aspect is non intrusive and can be done after the fact and then turned off when no longer required.
  • 异常处理:不需要重复 try ... catch、catch、catch 等可怕的列表——也意味着异常处理保证是一致的。
  • 性能监控:非常有用,因为使用方面是非侵入性的,可以在事后完成,然后在不再需要时关闭。

回答by Carles Barrobés

We use it for software license management, i.e. allow the software to run only if the computer has some specific license(s) installed. It is not that different from your listed uses, since it is a form of security check.

我们将其用于软件许可管理,即仅当计算机安装了某些特定许可时才允许软件运行。它与您列出的用途没有什么不同,因为它是一种安全检查形式。

I published a blog entry describing a practical implementation here

在这里发表了一篇描述实际实现的博客文章

回答by Adrian Rus

Runtime checking of code contracts. Code Contracts for .NETuse AOP for

代码契约的运行时检查。.NET 的代码契约使用 AOP

Runtime Checking. Our binary rewriter modifies a program by injecting the contracts, which are checked as part of program execution.

运行时检查。我们的二进制重写器通过注入合约来修改程序,合约作为程序执行的一部分进行检查。

回答by Espen

I will also recommend aspects for:

我还将推荐以下方面:

  • Async method calls
  • Monitoring
  • 异步方法调用
  • 监控

With Spring and tcServer (developer), you can easily monitor all your Spring beans with @Component annotation. You can see time used, the input and return data including exceptions.

使用 Spring 和 tcServer(开发人员),您可以使用 @Component 注释轻松监控所有 Spring bean。您可以看到使用的时间、输入和返回数据,包括异常。

回答by Turing Complete

INotifyPropertyChanged and similar horrors.

INotifyPropertyChanged 和类似的恐怖。

Basically wherever there is code that looks like this - use an aspect and you are done.

基本上只要有看起来像这样的代码 - 使用方面就完成了。