50

First let me note, that I use AspectJ and I like it, but what else can I do with it.

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.

But I believe AspectJ and AOP in general, can be used for more than: logging, transaction controlling, and simulation partial classes.

So what are other useful use cases for AspectJ and AOP?

True Soft
  • 8,675
  • 6
  • 54
  • 83
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • 2
    You should read AspectJ in Action by Ramnivas Laddad http://www.manning.com/laddad2 The TOC will give you an idea: http://www.manning.com/laddad2/excerpt_contents.html – Sean Patrick Floyd Nov 30 '10 at 12:40
  • 2
    @George Stocker: the question has 14 upvotes, 0 downvotes, 3 answers with in total 21 upvotes, and YOU put it on hold after 4 years - really? The point is that this question has good answers, so it does not really have this "too broad" problem. – Ralph Aug 10 '14 at 12:09
  • 1
    If you'd like to debate closure, feel free to bring it up on meta. Closing as too broad is independent of votes, views, or current answers. It has to do with the scope of the question. – George Stocker Aug 10 '14 at 12:37
  • @George Stocke: I have asked a question about what is the line between broad and too broad - http://meta.stackoverflow.com/questions/268305/what-is-an-indicator-that-a-question-is-too-broad-or-not – Ralph Aug 10 '14 at 14:11
  • 2
    Another valuable question closed as too broad... – Dave Oct 08 '14 at 13:23

3 Answers3

42
  • 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.
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 2
    Caching is another use – ex0b1t Feb 11 '15 at 10:13
  • Thanks, @ex0b1t. Any list will never be full. – AlexR Feb 12 '15 at 12:15
  • What about lots of null checks at methods begin. e.g.: I have interface with method doSmth(List list, OtherClass otherparam). And it will be implemented in a lot of classes. I need check in method doSmth if arguments are null in 90% classes which implemented interface. Should I use aspects? – HereAndBeyond May 07 '19 at 11:12
21

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.

Guillaume
  • 18,494
  • 8
  • 53
  • 74
8

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:

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)

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383