AOP 可能的用例有哪些?

发布于 2024-08-20 22:23:06 字数 321 浏览 1 评论 0原文

我想描绘一下 AOP 有效参与应用程序设计的可能情况。到目前为止我所遇到的只是:

  • 与日志记录相关的
  • 安全检查
  • 事务管理
  • 遗留应用程序的

调整还有其他吗?

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

相关问题)

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:

  • 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.)

(Related question)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

丢了幸福的猪 2024-08-27 22:23:06

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

  • 在 JMX 中自动注册对象以进行远程管理。如果一个类使用 @AutoRegister 注释进行注释,我们就有一个方面可以监视该类的新实例并自动将它们注册到 JMX 中。

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

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

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 @AutoRegister annotation, 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)
深海夜未眠 2024-08-27 22:23:06
  • 异常处理:不需要重复可怕的 try ... catch、catch、catch 等列表 - 也意味着异常处理保证是一致的。
  • 性能监控:非常有用,因为使用方面是非侵入性的,可以在事后完成,然后在不再需要时关闭。

哇... 10 年前 - 没有太多 AOP... 这里还有一些

  • 能够自定义您无权访问其构造函数的对象(例如 jpa 实体)
  • 实施安全规则(安全性说用户不允许调用此方法 - AOP 可以实现该方法)
  • 事务管理器(开始、提交、回滚)
  • 缓存 - 想要缓存方法的结果并且不再调用它
  • 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.

Wow... 10 years ago - didn't have much for AOP... Here are a few more

  • Be able to customise objects where you don't have access to their constructor (e.g. jpa entities)
  • Implementing security rules (security says user is not allowed to call this method - AOP can implement that)
  • Transaction manager (begin, commit, rollback)
  • Caching - want to cache the result of a method and not call it again
水中月 2024-08-27 22:23:06

要了解 AOP 在适用性方面的覆盖范围,我强烈建议您阅读面向方面的书-软件开发用例。本书详细阐述了使用 AOP 的功能性和非功能性需求的用例。之后,您将看到方面可以用于满足比日志记录、跟踪、安全性等更多的需求。

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.

累赘 2024-08-27 22:23:06

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

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.

〗斷ホ乔殘χμё〖 2024-08-27 22:23:06
  • 读/写锁。我没有复制相同的代码片段,而是使用一个方面来定义需要读锁或独占锁的方法。
  • 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.
秋意浓 2024-08-27 22:23:06

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

我在此处发布了一篇描述实际实现的博客文章

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

╭⌒浅淡时光〆 2024-08-27 22:23:06

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

相关文章你一定要看看。

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

Related articles you must look at.

情魔剑神 2024-08-27 22:23:06

我还将推荐以下方面:

  • 异步方法调用
  • 监控

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

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.

鼻尖触碰 2024-08-27 22:23:06

INotifyPropertyChanged 和类似的恐怖。

基本上只要有类似这样的代码 - 使用一个方面就可以了。

INotifyPropertyChanged and similar horrors.

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

难以启齿的温柔 2024-08-27 22:23:06

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

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

Runtime checking of code contracts. Code Contracts for .NET use AOP for

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

⊕婉儿 2024-08-27 22:23:06

我们使用AspectJ来完成AOP。除了上面提到的用例之外,用例如下:

  • 将方法调用的访问限制为仅少数类。
  • 自动注释选定的方法/类/字段。

We use AspectJ to accomplish AOP. Use cases apart from the above mentioned ones are as follows:

  • Restricting access to method calls to only few classes.
  • Automatically annotating selected methods/classes/fields.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文