AOP 可能的用例有哪些?
我想描绘一下 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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我可以给你举两个使用它的例子:
在 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:
哇... 10 年前 - 没有太多 AOP... 这里还有一些
Wow... 10 years ago - didn't have much for AOP... Here are a few more
要了解 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.
方法级缓存,如果您的方法是无状态的(我的意思是使用相同的参数值重复调用时返回相同的值)。这对于 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.
我们将其用于软件许可证管理,即仅当计算机安装了某些特定许可证时才允许软件运行。它与您列出的用途没有什么不同,因为它是一种安全检查形式。
我在此处发布了一篇描述实际实现的博客文章
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
除了您列出的所有用途之外,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.
我还将推荐以下方面:
使用 Spring 和 tcServer(开发人员),您可以轻松监控所有 Spring beans
@Component注解。您可以查看使用的时间、输入和返回数据(包括异常)。
I will also recommend aspects for:
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.
INotifyPropertyChanged 和类似的恐怖。
基本上只要有类似这样的代码 - 使用一个方面就可以了。
INotifyPropertyChanged and similar horrors.
Basically wherever there is code that looks like this - use an aspect and you are done.
代码契约的运行时检查。 .NET 代码协定使用 AOP
Runtime checking of code contracts. Code Contracts for .NET use AOP for
我们使用AspectJ来完成AOP。除了上面提到的用例之外,用例如下:
We use AspectJ to accomplish AOP. Use cases apart from the above mentioned ones are as follows: