Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is AspectJ good for? [closed]

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?

like image 379
Ralph Avatar asked Nov 30 '10 12:11

Ralph


People also ask

What is AspectJ used for?

AspectJ is very mature, powerful and widely used in today's enterprise Java frameworks like Spring. Spring uses AspectJ to make it easy for the developers to use Transaction Management, or applying security (using Spring Security) in your application.

What is the use of AspectJ in Spring?

@AspectJ refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations. The @AspectJ support is enabled by including the following element inside your XML Schema-based configuration file.

What does AspectJ Autoproxy do?

@AspectJAutoProxy. Fashioned after Spring XML's <aop:aspectj-autoproxy> , @AspectJAutoProxy detects any @Aspect beans and generates proxies as appropriate to weave the advice methods in those aspects against other beans in the container.

What does AspectJ Weaver do?

An aspect weaver takes information from raw classes and aspects and creates new classes with the aspect code appropriately weaved into the classes.


2 Answers

  • 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.
like image 198
AlexR Avatar answered Sep 17 '22 15:09

AlexR


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.

like image 25
Guillaume Avatar answered Sep 17 '22 15:09

Guillaume