java spring - What are the possible AOP use cases?

oriented of (10)

I'd like to make a picture of what are the possible cases for effective involvement of AOP in application design. All I have met so far is:

  • logging-related
  • security checks
  • transaction management
  • tweaking of a legacy application

Anything else?

(It doesn't have to be necessarily Spring's proxy based AOP - rather JBoss AOP.)

(Related question)


I will also recommend aspects for:

  • Async method calls
  • Monitoring

With Spring and tcServer (developer), you can easily monitor all your Spring beans with @Component annotation. You can see time used, the input and return data including exceptions.

Method level caching,if your method is stateless(I mean returns same value when invoked repeatedly with same parameter values). This is more effective in case of DAO methods because it avoids database hit.

  • Exception Handling: don't need to repeat the horrible list of try ... catch, catch, catch etc - also means the exception handling is guaranteed to be consistent.
  • Performance monitoring: Very useful as using an aspect is non intrusive and can be done after the fact and then turned off when no longer required.

  • Read/write locks. Instead of replicating the same snippet, I used an aspect to define the methods that needed a read lock or an exclusive lock.

Runtime checking of code contracts. Code Contracts for .NET use AOP for

Runtime Checking. Our binary rewriter modifies a program by injecting the contracts, which are checked as part of program execution.

I can give you two examples where we use it:

  • Automatically registering objects in JMX for remote management. If a class is annotated with our @AutoRegister annotation, we have an aspect that watches for new instantiations of that class and registers them in JMX automatically.

  • Audit logging (the gold standard AOP use case). Its a bit coarse but the general approach is to annotate methods that represent some auditable action. Combined with something like Spring Security, we can get a pretty good idea of:

    • who the user is
    • what method they're invoking
    • what data they're providing
    • what time the method was invoked
    • whether the invocation was successful or not (i.e., if an exception was thrown)

We use AspectJ to accomplish AOP. Use cases apart from the above mentioned ones are as follows:

  • Restricting access to method calls to only few classes.
  • Automatically annotating selected methods/classes/fields.

INotifyPropertyChanged and similar horrors.

Basically wherever there is code that looks like this - use an aspect and you are done.

simple example for reflection. In a Chess Game, you do not know what will be moved by the user at run time. reflection can be used to call methods which are already implemented at run time.

public class Test {

    public void firstMoveChoice(){
        System.out.println("First Move");
    public void secondMOveChoice(){
        System.out.println("Second Move");
    public void thirdMoveChoice(){
        System.out.println("Third Move");

    public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { 
        Test test = new Test();
        Method[] method = test.getClass().getMethods();
        method[0].invoke(test, null);
        method[1].invoke(test, null);
        method[2].invoke(test, null);


java spring jboss aop use-case