Professional Documents
Culture Documents
html
Advices, pointcuts. Obscure words making me wonder what Aspect-Oriented Programming (AOP) is all about. In fact,
behind these words lie very simple principles. Once the AOP vocabulary has been tackled, using AOP is a matter of practice.
Spring offers different techniques to use AOP, but I think that the variety of choice makes AOP even more confusing. Let's
clear this up, and see how AOP can be used in Spring.
Two AOP implementations can be chosen, as well as two coding styles for each. Confusing, isn't it ?
Spring AOP. It is not aimed to provide a full AOP implementation but it will be more than enough in most cases.
using the @AspectJ annotation style
using the XML configuration style
AspectJ. If Spring AOP is not enough, AspectJ, a Java implementation of AOP, can be easily plugged in.
using the AspectJ language syntax
using the @AspectJ annotation style
Annotations can be used with Java5+. I recommend to go through Spring's reference documentation to decide which one to
choose. It will mainly depend on requirements and taste. I think that understanding Spring AOP is a priority before thinking
of using AspectJ. In this tutorial, we will use Spring AOP only, using both the @AspectJ and XML styles.
It is assumed that you are already comfortable with Spring's dependency injection. If not, you can read this tutorial first. Basic
database knowledge will help too.
[Before]
[After]
1 de 6 23/06/2014 19:01
What It Means To Mock http://www.javaranch.com/journal/2008/04/spring-aop-proxies.html
...
// Insert item into ORDER table
...
// Update ITEM table, decrement the number of remaining items
...
// Commit changes
...
}
}
All this changes to make one transaction ! And that's just the orderItem() method. All methods that need transactions will
look like this. The OrderServiceImpl's primary objective is only to fetch, insert, update or delete users. It doesn't care about
transactions after all. If transactions could magically start when needed, without the OrderServiceImpl knowing it. Wouldn't it
be great if transactions could be implemented without touching the original object ? That is where AOP comes to the rescue.
A transaction is a crosscutting concern. It may be applied anywhere on top of existing code. Spring not only supports AOP,
but also offers a very useful transaction management system. But before diving into AOP, let's first see an important concept
in Spring AOP: proxies.
Proxy
A proxy is a well-used design pattern. To put it simply, a proxy is an object that looks like another object, but adds special
functionality behind the scene. In Spring, any interface can be proxied. Let's see a simple example to illustrate what a proxy
really is. The Saloon interface:
package com.javaranch.journal.spring.aop;
void openSaloon();
}
package com.javaranch.journal.spring.aop;
2 de 6 23/06/2014 19:01
What It Means To Mock http://www.javaranch.com/journal/2008/04/spring-aop-proxies.html
<bean id="proxySaloon"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"><value>com.javaranch.journal.spring.aop.Saloon</value></property>
<property name="target"><ref local="mooseSaloon"/></property>
</bean>
</beans>
The Main class using both the MooseSaloon and its proxy:
package com.javaranch.journal.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
Running this application will produce something similar to the following output :
You can notice that both the MooseSaloon and its proxy do the same thing. However, both mooseSaloon and proxySaloon are
different classes. The proxy holds a reference to the mooseSaloon and implements the same interface, thus it holds the same
methods implemented by MooseSaloon. What you cannot see yet is that the proxy can add some nifty functionality behind
the hood. But before getting deeper into AOP, let's see some important terminology.
AOP jargon
The AOP terminology may be boring to get on with, but it is necessary to understand these words before going further.
Join point : a particular execution point. For example a method execution, or the handling of an exception.
Advice : simply said, it is the code to execute. For example, starting a transaction.
Pointcut : represents which join point an advice should be applied to
Aspect : pointcuts and advices form an aspect
Target object : the object being advised by some aspects
Weaving : "applying an aspect"
When an aspect is applied to an object, a particular advice will be applied to any join point matching a pointcut's expression.
Starting a transaction could be expressed the following way :
3 de 6 23/06/2014 19:01
What It Means To Mock http://www.javaranch.com/journal/2008/04/spring-aop-proxies.html
NOTE: In Spring AOP, only the "execute method" join point is supported.
A simple advice
In the MooseSaloon example, let's apply a simple aspect :
Don't worry about ProxyBeanFactory and MethodBeforeAdvice gorry details. The point here is just to illustrate what a proxy
does behind the scene. Here is the advice we will apply to our saloon.
package com.javaranch.journal.spring.aop;
import java.lang.reflect.Method;
import java.util.Date;
import org.springframework.aop.MethodBeforeAdvice;
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("[" + new Date() + "]" + method.getName() + " called on " + target);
}
}
The MethodBeforeAdvice's before() method will be called before a method of a target bean is called.
<bean id="proxySaloon"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"><value>com.javaranch.journal.spring.aop.Saloon</value></property>
<property name="target"><ref local="mooseSaloon"/></property>
<property name="interceptorNames">
<list>
<value>saloonWatcher</value>
</list>
</property>
</bean>
That's all. The Main class and the MooseSaloon class do not change. After execution, the printed information will look like
this:
Isn't that magic ? Not really, it's called AOP, using a proxy. Although the MooseSaloon class was not changed, we managed to
report when a Saloon method is called. Can you now imagine how to manage transactions without modifying the existing
code ?
4 de 6 23/06/2014 19:01
What It Means To Mock http://www.javaranch.com/journal/2008/04/spring-aop-proxies.html
package com.javaranch.journal.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
And let's remove it from the definition file too. Instead, we will use the BeanNameAutoProxyCreator to create a proxy
automatically on the MooseSaloon.
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames"><value>*Saloon</value></property>
<property name="interceptorNames">
<list>
<value>saloonWatcher</value>
</list>
</property>
</bean>
</beans>
A proxy will be created for every bean whose name ends in "Saloon", just like our "mooseSaloon". After executing this new
version, something similar to the following should be printed:
Amazing, isn't it ? Look at the Main class. Although the "mooseSaloon" bean is obtained from the factory, the log information
has been printed out. The class name is not MooseSaloon, but "$Proxy0". This shows that we are working on the proxy, not
the bean itself.
You've made it
Congratulations, you've made it ! Don't think too much about MethodBeforeAdvice, ProxyBeanFactory or
5 de 6 23/06/2014 19:01
What It Means To Mock http://www.javaranch.com/journal/2008/04/spring-aop-proxies.html
BeanNameAutoProxyCreator. These were used in Spring 1.2, but there are now other ways to use proxies and AOP since
Spring 2.0, which will be the subject of this tutorial's next part. If you have understood what a proxy is, and still remember
what the AOP main keywords are, you're fine for now.
6 de 6 23/06/2014 19:01