What is AspectJ good for? [closed]
Asked Answered
E

3

51

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?

Empoverish answered 30/11, 2010 at 12:34 Comment(5)
You should read AspectJ in Action by Ramnivas Laddad manning.com/laddad2 The TOC will give you an idea: manning.com/laddad2/excerpt_contents.htmlCaught
@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.Empoverish
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.Saltarello
@George Stocke: I have asked a question about what is the line between broad and too broad - meta.#268805Empoverish
Another valuable question closed as too broad...Laryngotomy
I
43
  • 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.
Immunogenetics answered 30/11, 2010 at 12:48 Comment(3)
Caching is another useBearish
Thanks, @ex0b1t. Any list will never be full.Immunogenetics
What about lots of null checks at methods begin. e.g.: I have interface with method doSmth(List<Objects> 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?Nolie
C
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.

Chrissie answered 30/11, 2010 at 12:43 Comment(0)
E
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)

Empoverish answered 2/5, 2011 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.