AOP is a pattern used to modularize cross cutting features. So if there is a certain "thing" that applies to a significant part in your code then you can use AOP to solve that problem. These "things" are called aspects.
An example below:
An exception logger is used across the enterprise app. So you could set it up using AOP the following way. So now all methods under my.service package will get logged the following way.
And the ExceptionLogger class could be something like below:-
public class ExceptionLogger {
private static Logger logger = Logger.getLogger(ExceptionLogger.class);
public void logIt(JoinPoint jp, Exception e) {
StringBuilder msg = new StringBuilder();
msg.append("");
logger.error(msg.toString());
}
}
Also take a look at this relevant question:-
- What is the most common use for AOP in spring project