Inject Mocks in classes that otherwise would create new instances by using new. Assume you have this code:
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 AspectJ by Daniel Roop, as well as Spring Roo`s @MockStaticEntityMethods (Mock Static Methods using Spring Aspect)
Wikipedia 条目 为您提供了更多示例(但不是很多)。通常,面向方面的编程应该仅用于实现不属于类的核心关注点并且对于不同类来说是公共的简单行为。一旦你开始在方面放入太多逻辑,代码就会变得非常难以阅读。
您建议的方面(日志记录、事务……)是最常用的。我也会增加安全性。
The Wikipedia entry gives 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.
修复您无法更改的 API 的错误或行为。例如,在某些情况下返回 false 的布尔方法,但应返回 true。您可以使用 AspectJ 修复此问题。
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 return true. You can fix this using AspectJ.
发布评论
评论(3)
人们可以使用 AspectJ 来强制执行一些(设计)规则。
在类中注入 Mock,否则这些类将使用 new 创建新实例。
假设您有以下代码:
并且需要将
email
替换为模拟。然后您可以使用 Aspect (@Pointcut("call(InvitationEmail.new(..))")
) 来“注入”一个模拟。 -- @参见 Daniel Roop 的 博客 JMock 和 AspectJ,以及 Spring Roo 的 @MockStaticEntityMethods (使用 Spring 的模拟静态方法方面)One can use AspectJ for enforcing some (design) rules.
Inject Mocks in classes that otherwise would create new instances by using new.
Assume you have this code:
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 AspectJ by Daniel Roop, as well as Spring Roo`s @MockStaticEntityMethods (Mock Static Methods using Spring Aspect)Wikipedia 条目 为您提供了更多示例(但不是很多)。通常,面向方面的编程应该仅用于实现不属于类的核心关注点并且对于不同类来说是公共的简单行为。一旦你开始在方面放入太多逻辑,代码就会变得非常难以阅读。
您建议的方面(日志记录、事务……)是最常用的。我也会增加安全性。
The Wikipedia entry gives 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.
后处理结果编辑
尽管自从我给出以来已经过去了很多年这个答案我决定添加以下内容以使答案更完整。
false
的布尔方法,但应返回true
。您可以使用 AspectJ 修复此问题。EDIT
Although many years passed since I gave this answer I decided to add the following to make the answer more complete.
false
in some conditions but should returntrue
. You can fix this using AspectJ.