What are the possible AOP use cases?
- 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.
Wow... 10 years ago - didn't have much for AOP... Here are a few more
- Be able to customise objects where you don't have access to their constructor (e.g. jpa entities)
- Implementing security rules (security says user is not allowed to call this method - AOP can implement that)
- Transaction manager (begin, commit, rollback)
- Caching - want to cache the result of a method and not call it again
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)