public interface Pointcut {
ClassFilter getClassFilter();
MethodMatcher getMethodMatcher();
}
public interface ClassFilter {
boolean matches(Class clazz);
}
public interface MethodMatcher {
boolean matches(Method m, Class targetClass);
boolean matches(Method m, Class targetClass, Object[] args);
boolean isRuntime();
}
<bean id="gettersAndSettersPointcut"
class="org.springframework.aop.support.RegexpMethodPointcut">
<property name="patterns">
<list>
<value>.*/.get.*</value>
<value>.*/.set.*</value>
</list>
</property>
</bean>
public interface MethodInterceptor extends Interceptor {
Object invoke(MethodInvocation invocation) throws Throwable;
}
public class DebugInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation)
throws Throwable {
System.out.println(">> " + invocation); // before
Object rval = invocation.proceed();
System.out.println("<< Invocation returned"); // after
return rval;
}
}
<bean id="gettersAndSettersAdvisor"
class="...aop.support.RegexpMethodPointcutAroundAdvisor">
<property name="interceptor">
<ref local="interceptorBean"/>
</property>
<property name="patterns">
<list>
<value>.*/.get.*</value>
<value>.*/.set.*</value>
</list>
</property>
</bean>
ProxyFactory factory = new ProxyFactory(myBusinessInterfaceImpl);
factory.addInterceptor(myMethodInterceptor);
factory.addAdvisor(myAdvisor);
MyBusinessInterface b = (MyBusinessInterface)factory.getProxy();
<bean id="personTarget" class="eg.PersonImpl">
<property name="name"><value>Tony</value></property>
<property name="age"><value>51</value></property>
</bean>
<bean id="myAdvisor" class="eg.MyAdvisor">
<property name="someProperty"><value>Something</value></property>
</bean>
<bean id="debugInterceptor" class="...aop.interceptor.NopInterceptor">
</bean>
<bean id="person" class="...aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"><value>eg.Person</value></property>
<property name="target"><ref local="personTarget"/></property>
<property name="interceptorNames">
<list>
<value>myAdvisor</value>
<value>debugInterceptor</value>
</list>
</property>
</bean>
<bean id="personUser" class="com.mycompany.PersonUser">
<property name="person"><ref local="person" /></property>
</bean>
Person person = (Person) factory.getBean("person");
<bean id="person" class="...aop.framework.ProxyFactoryBean">
<property name="proxyTargetClass"><value>true</value></property>
<property name="target"><ref local="personTarget"/></property>
<property name="interceptorNames">
<list>
<value>myAdvisor</value>
<value>debugInterceptor</value>
</list>
</property>
</bean>
<bean id="employee1" class="eg.Employee">...</bean>
<bean id="employee2" class="eg.Employee">...</bean>
<bean id="myInterceptor" class="eg.DebugInterceptor"/>
<bean id="beanNameProxyCreator"
class="...aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames"><value>employee*</value></property>
<property name="interceptorNames">
<list>
<value>myInterceptor</value>
</list>
</property>
</bean>
<bean id="debugInterceptor" class="app.DebugInterceptor"/>
<bean id="getterDebugAdvisor"
class="...aop.support.RegexpMethodPointcutAdvisor">
<constructor-arg>
<ref bean="debugInterceptor"/>
</constructor-arg>
<property name="pattern"><value>.*/.get.*</value></property>
</bean>
<bean id="autoProxyCreator"
class="...aop.framework.autoproxy.AdvisorAutoProxyCreator">
<property name="proxyTargetClass"><value>true</value></property>
</bean>