Around advice:
MethodInterceptor provides the ability to do both in one advice object.
the MethodInterceptor implementation controls whether the target method is actually invoked.
Throws advice:
ThrowsAdvice lets you define behavior should an exception occur.
ThrowsAdvice is a marker interface and contains no methods that need to be implemented.
Instead, a class that implements this interface must have at least one method with either of the following two signatures:
void afterThrowing(Throwable throwable)
void afterThrowing(Method method, Object[] args, Object target, Throwable throwable)
You can also have more than one afterThrowing method in the same class.
After the ThrowsAdvice is executed, the original exception will still be thrown and will propagate up the stack like any other exception. The only way your ThrowsAdvice can change this is to throw another exception.
Pointcuts determine if a particular method on a particular class matches a particular criterion.
Defining a pointcut in Spring:
The core interface for Spring’s pointcut framework is, naturally, the Pointcut interface.
public interface Pointcut {
ClassFilter getClassFilter();
MethodMatcher getMethodMatcher();
}
public interface ClassFilter {
boolean matches(Class clazz);
}
public interface MethodMatcher {
boolean matches(Method m, Class targetClass);
public boolean isRuntime();
public boolean matches(Method m, Class target, Object[] args);
}
The matches(Method, Class) method determines whether a method is a candidate to be advised based on the target Class and Method. Since this can be determined statically, this method is only called once—when the AOP proxy is created.
If matches(Method, Class) returns true, isRuntime() is called to determine what type of MethodMatcher this is. There are two types: static and dynamic.
Although you can implement the Pointcut interface yourself, you will most likely use one of Spring’s predefined Pointcut implementations.
public interface PointcutAdvisor {
Pointcut getPointcut();
Advice getAdvice();
}
Most of Spring’s built-in pointcuts also have a corresponding PointcutAdvisor.This is convenient if you want to define a pointcut and the advice it is managing in one place.
static pointcuts are preferred because they perform better than dynamic pointcuts since they are evaluated once.
Spring provides a convenience superclass for creating static pointcuts: StaticMethodMatcherPointcut. So if you want to create a custom static pointcut, you can override this class and implement the isMatch method.
The most basic of these is the NameMatchMethodPointcut. This class has two methods you should be interested in:
public void setMappedName(String)
public void setMappedNames(String[])
Note that this matching only applies to the method name itself, not the fully qualified name that includes that class name as well.
<beans>
<bean id="maidServiceTarget" class="com.springinaction.chapter03.cleaning.MaidServiceImpl"/>
<bean id="frequentCustomerAdvice" class="com.springinaction.chapter03.cleaning.FrequentCustomerAdvice"/>
<bean id="frequentCustomerPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName">
<value>order*</value>
</property>
<property name="advice">
<ref bean="frequentCustomerAdvice"/>
</property>
</bean>
<bean id="maidService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.springinaction.chapter03.cleaning.MaidService</value>
</property>
<property name="interceptorNames">
<list>
<value>frequentCustomerAdvisor</value>
</list>
</property>
<property name="target">
<value ref="maidServiceTarget">
</property>
</bean>
</beans>