java AspectJ 有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4313789/
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
What is AspectJ good for?
提问by Ralph
First let me note, that I use AspectJ and I like it, but what else can I do with it.
首先让我注意,我使用 AspectJ 并且我喜欢它,但是我还能用它做什么。
I know AspectJ can be/is used for Logging. In some cases it is used for Transaction controlling – mostly implemented in conjunction with annotations. AspectJ can also be used to enhance classes with (code-generated) methods, like Spring Roo does.
我知道 AspectJ 可以/正在用于日志记录。在某些情况下,它用于事务控制——主要与注释一起实现。AspectJ 也可用于使用(代码生成的)方法来增强类,就像 Spring Roo 那样。
But I believe AspectJ and AOP in general, can be used for more than: logging, transaction controlling, and simulation partial classes.
但我相信 AspectJ 和 AOP 大体上可以用于:日志记录、事务控制和模拟部分类。
So what are other useful use cases for AspectJ and AOP?
那么 AspectJ 和 AOP 的其他有用用例是什么?
采纳答案by AlexR
- permission check
- interrupt action that takes too long
- run action in separate thread or even in context of different process or event on other machine
- monitoring
- preparing any data / environment before call and processing results after call
- opening / closing resources
- 权限检查
- 中断耗时过长的动作
- 在单独的线程中甚至在其他机器上的不同进程或事件的上下文中运行操作
- 监控
- 在调用前准备任何数据/环境并在调用后处理结果
- 打开/关闭资源
EDIT
编辑
Although many years passed since I gave this answer I decided to add the following to make the answer more complete.
尽管我给出这个答案已经过去了很多年,但我还是决定添加以下内容以使答案更加完整。
- security check.
- fixes of incorrect or behavior of API that you cannot change. For example boolean method that returns
false
in some conditions but should returntrue
. You can fix this using AspectJ.
- 安全检查。
- 您无法更改的 API 错误或行为的修复。例如,
false
在某些条件下返回但应该返回的布尔方法true
。您可以使用 AspectJ 解决此问题。
回答by Guillaume
The Wikipedia entrygives you a few more examples (but not that many). Typically, Aspect Oriented Programing should be use only to implement simple behaviours that are not part of core concern of a class and are common to different classes. As soon as you begin to put too much logic in your aspects, the code becomes really unreadable.
在维基百科条目给你一些例子(但不是很多)。通常,面向方面的编程应该仅用于实现不属于类核心关注的部分并且对不同类通用的简单行为。一旦您开始在方面中放入过多的逻辑,代码就会变得非常不可读。
The aspect you suggest (logging, transaction, ...) are the most commonly used. I would add security as well.
您建议的方面(日志记录、事务等)是最常用的。我也会增加安全性。
回答by Ralph
One can use AspectJ for enforcing some (design) rules.
可以使用 AspectJ 来强制执行某些(设计)规则。
- like every controller method need some special annotations
- every service/frontend/dto class must be located in a service/fronten/dto pacakge
- more mature thinks like: checking that setters do not have any logic.
- 就像每个控制器方法都需要一些特殊的注释
- 每个 service/frontend/dto 类都必须位于 service/fronten/dto pacakge 中
- 更成熟的想法是:检查 setter 没有任何逻辑。
Inject Mocks in classes that otherwise would create new instances by using new. Assume you have this code:
在类中注入 Mock,否则会使用 new 创建新实例。假设您有以下代码:
public void sendInvitationEmail(String address) {
InvitationEmail email = new InvitationEmail();
email.sendTo(address).send();
}
And need to replace email
by an mock. Then you could use an Aspect (@Pointcut("call(InvitationEmail.new(..))")
) to "inject" a mock. -- @See Blog JMock and AspectJby Daniel Roop, as well as Spring Roo`s @MockStaticEntityMethods (Mock Static Methods using Spring Aspect)
并且需要替换email
为模拟。然后你可以使用 Aspect ( @Pointcut("call(InvitationEmail.new(..))")
) 来“注入”一个模拟。-- @See博客 JMock 和Daniel Roop 的AspectJ,以及 Spring Roo 的 @MockStaticEntityMethods(使用 Spring Aspect 模拟静态方法)