“One of the key components of Spring is the AOP framework. While the Spring IoC container does not depend on AOP, meaning you do not need to use AOP if you don't want to, AOP complements Spring IoC to provide a very capable middleware solution. AOP is used in the Spring Framework to...
- ... provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative transaction management.
- ... allow users to implement custom aspects, complementing their use of OOP with AOP.”
Nevertheless Spring AOP framework comes with certain limitations in comparison to a complete AOPimplementation, such as AspectJ. The most common problems people encounter while working withSpring AOP framework derive from the fact that Spring AOP is “proxy – based”. In other words when a bean is used as a dependency and its method(s) should be advised by particular aspect(s) the IoC container injects an “aspect – aware” bean proxy instead of the bean itself. Method invocations are performed against the proxy bean instance, transparently to the user, in order for aspect logic to be executed before and/or after delegating the call to the actual “proxy–ed” bean.
Furthermore Spring AOP framework uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. The first one can create proxies only for the interfaces whilst the second is able to proxy concrete classes but with certain limitations. In particular, as stated in Spring reference documentation,
“If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used. All of the interfaces implemented by the target type will be proxied. If the target object does not implement any interfaces then a CGLIB proxy will be created.”
To summarize, when working with Spring AOP framework you should have two important things in mind :
- If your “proxy–ed” bean implements at least one interface, the proxy bean can ONLY be casted to that interface(s). If you try to cast it to the “proxy–ed” bean class, then you should expect a ClassCastException to be thrown at runtime. Nevertheless Spring AOP framework provides the option to force CGLIB proxying but with the aforementioned limitations (please refer to the relevant chapter of Spring reference documentation)
- Aspects do not apply to intra–operation calls. Meaning that there is no way for the proxy to intercept the calling of a method originated from another method of the same “proxy–ed” bean
- compile time weaving – compile either target source or aspect classes via the AspectJ compiler
- post compile weaving – inject aspect instructions to already compiled classes
- load time weaving – inject aspect instructions to the byte code during class loading
Spring users who have already implemented aspects for their bean services can switch to AspectJtransparently, meaning that no special code needs to be written since Spring AOP framework uses a subset of AspectJ pointcut expression language, and @AspectJ Spring aspects are fully eligible forAspectJ weaving.
Our preferred development environment is Eclipse, so as a prerequisite you must have Eclipse withMaven support installed. The installation of Maven plugin for Eclipse is out of the scope of this tutorial and will not be discussed. Nevertheless you will need the following components :
We will be using Eclipse Galileo, „m2eclipse“ Maven Integration for Eclipse Plugin version 0.10.0, Spring version 3.0.1, aspectjrt version 1.6.7 and aspectj-maven-plugin version 1.3 for this tutorial.
Lets begin,
- Create a new Maven project, go to File → Project → Maven → Maven Project
- In the „Select project name and location“ page of the wizard, make sure that „Create a simple project (skip archetype selection)“ option is unchecked, hit „Next“ to continue with default values
- In the „Select an Archetype“ page of the wizard, select „Nexus Indexer“ at the „Catalog“ drop down list and after the archetypes selection area is refreshed, select the „webapp-jee5“ archetype from „org.codehaus.mojo.archetypes“ to use. You can use the „filter“ text box to narrow search results. Hit „Next“ to continue
- In the „Enter an artifact id“ page of the wizard, you can define the name and main package of your project. We will set the „Group Id“ variable to „com.javacodegeeks“ and the „Artifact Id“ variable to „aspectjspring“. The aforementioned selections compose the main project package as „com.javacodegeeks.aspectjspring “ and the project name as „aspectjspring“. Hit „Finish“ to exit the wizard and to create your project
No comments:
Post a Comment