More on AOP in Spring Part 1
by, 11-16-2011 at 06:03 PM (1381 Views)
In the previous tip, we started to look at Aspect Oriented Programming support in Spring. In this tip we will continue with this assessment of this exciting technology. Before we start there are a number of terms that I need to introduce to you in order that you can speak the lingo of AOP. I list these terms below:
- Advice - I had briefly mentioned this is the last tip. Advice is the code to be executed at a join point that has been selected by a pointcut. Advice can execute before, after, around a join point as well as after returning or after throwing.
- Join Point - This is an identifiable point in the execution of a program. It could be a method call or an assignment to a member of an object.
- Pointcuts - This is a program construct that selects join points and collects context at those points.
- Aspects - This is the central unit of AspectJ similar to how a class is the central unit of Java. It contains the code that expresses the weaving rules for both dynamic and static crosscutting.
- Introduction - This is a static crosscutting instruction that introduces changes to the classes, interfaces and aspects of the system. It makes static changes to the modules that do not directly affect their behavior such as an added method of field to a class.
- Weaving - This is the process of applying aspects to a target object to create a new proxy object. In AspectJ there are three weaving models:
- Source Weaving - Weaver is part of the compiler
- Binary Weaving - Input to weaver - classes and aspects - is in byte code form.
- Load-time Weaving - Takes binary classes and aspects and XML defined aspects and configuration.
- The aspects are integrated into the target object at the defined join points. The implementation of weaving rules by the compiler is called crosscutting. There are two types of crosscutting:
[+]Dynamic crosscutting - the weaving of new behavior into the execution of a program.
[+]Static crosscutting - the weaving of modifications into the static structure (i.e. classes, interfaces, and aspects) of the system. It is most commonly used to support the implementation of dynamic crosscutting.
Now that we outlined the key terms in Spring, we can look at how Spring supports these terms. Aspects are woven at runtime into Spring-managed beans. It does this by wrapping them in a proxy class. The proxy class assumes the role of the target bean. It intercepts the advised method calls and forwards these calls to the target bean. Between the time when the proxy intercepts the method call and the time when it invokes the target beanís method, the proxy performs the aspect logic.
Spring only creates a proxy object when it is needed by the application. If you use the ApplicationContext, the proxied objects will be created when it loads all of the beans from the BeanFactory. Because Spring creates the required proxies at runtime. There is no need for special compilers to take advantage of AOP.
You can also do multiple join point models through various AOP implementations. Unfortunately because Spring is based on dynamic proxies, it presently only supports method join points. This means that you cannot do field and constructor join points in addition to method pointcuts. Springís lack of field pointcuts prevents you from creating very fine-grained advice, such as intercepting updates to an objectís field as well as preventing you from applying advice when the bean is instantiated using constructor pointcuts.