Spring / Spring AOP
Aspect-oriented programming (AOP) refers to a programming technique that allows programmers to modularize crosscutting concerns or behavior that cuts across the typical divisions of responsibility, such as logging and transaction management.
The core construct of AOP is the aspect that encapsulates behaviors affecting multiple classes into reusable modules.
It is a module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement. In spring AOP, aspects are implemented using regular classes annotated with @Aspect annotation (@AspectJ style).
The concern is behavior that we want to have in a particular module of an application. A concern may be defined as a functionality we want to implement.
The cross-cutting concern is a concern which is applicable throughout the application and it affects the entire application. For example, logging, security and data transfer is applicable for every module of an application hence they are cross-cutting concerns.
The Join point represents a point in an applicaiton where we can plug-in an AOP aspect. It is the actual place in the application where an action will be taken using Spring AOP framework.
The advice is the actual action that will be taken either before or after the method execution. This is the actual piece of code that is invoked during the program execution by the Spring AOP framework.
Spring aspects can work with five different advices.
- before: Run advice before the a method execution.
- after: Run advice after the a method execution regardless of its outcome.
- after-returning: Run advice after the a method execution only if method completes successfully.
- after-throwing: Run advice after the a method execution only if method exits by throwing an exception.
- around: Run advice before and after the advised method is invoked.
The pointcut is a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns.
An Introduction allows us to add new methods or attributes to existing classes.
The target object is an object being advised by one or more aspects. It will always be a proxy object. It is also referred to as the advised object.
A proxy is an object that is created after applying advice to a target object. when you think of client objects the target object and the proxy object are the same.
- BeanNameAutoProxyCreator,
- DefaultAdvisorAutoProxyCreator,
- and Metadata autoproxying.
Weaving is the process of linking aspects with other application types or objects to create an advised object. Weaving can be done at compile time, at load time, or at runtime.
In this implementation case, aspects are implemented using regular classes along with XML based configuration.
This implementation case (@AspectJ based implementation) refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations.
There are 5 types of Advice.
- Before Advice. Advice that is executed prior to a joinpoint.
- After returning advice. Advice that is executed after the normal completion of a joinpoint.
- After throwing advice. Advice that is executed only if a method exits abnormally by throwing an exception.
- Finally advice. Advice that is executed irrespective of how a joinpoint exit.
- Around advice. Advice that borders a joinpoint, for example, a method invocation. Advice can execute before and after the invocation of method.
AOP alliance is an open-source project which is aimed at promoting adoption of AOP. The AOP alliance?s goal is to define a common set of components and interfaces so as to improve interoperability among different AOP implementations.
To force the use of CGLIB proxies set the value of the proxy-target-class attribute of the
<aop:config proxy-target-class="true"> <!-- other beans defined here... --> </aop:config>
Spring AOP is proxy-based. Spring uses either JDK proxy, preferred wheneven the proxied target implements at least one interface or CGLIB proxy when the target object does not implement any interfaces, to create the proxy for a given target bean.
Spring AOP performs run-time weaving. You can however set up Spring to do load-time weaving through AspectJ.AOP is mainly based on Proxy design pattern. It also uses remoting, Singleton, template method and decorator design pattern.
Spring AOP does runtime weaving by default while Aspect is compile time weaving.
Spring AOP doesnt work with final class or final/static methods because it may not able to create proxy by subclassing. AspectJ works with final class as it does compile time weaving and not depend on proxy pattern.
- Spring AOP,
- JBoss AOP,
- Apache AspectJ.