1.前置通知
public interface MethodBeforeAdvice{
void before(Method method, Object[] args,Object target)throws Throwable;
}
public class WelcomeAdvice implements MethodBeforeAdvice{
public void before(Method method, Object[] args, Object target){
Customer customer =(Customer) args[0];
System.out.println("Hello "+customer.getName() +" .How are you doing?");
}
}
<beans>
<bean id="**Target" class="****.**"/>
<bean id="welcomeAdvice" class="*****.WelcomeAdvice"/>
<bean id="**" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces ">
<value>interface of the target class</value>
</property>
<property name="interceptorNames ">
<list>
<value>welcomeAdvice</value>
</list>
</property>
<property name="target ">
<ref bean="**Target"/>
</property>
</bean>
</beans>
public interface AfterReturningAdvice{
void afterReturning(Object returnValue,Method method, Object[] args,Object target)throws Throwable;
}
public class ThankYouAdvice implements AfterReturningAdvice{
public void afterReturning (Object returnValue, Method method, Object[] arg2,Object target)throws Throwable{
System.out.println("Thank you.");
}
}
public interface MethodInterceptor extends Interceptor{
Object invoke (MethodInvocation invocation )throws Throwable;
}
MethodInterceptor 接口和前面的2种通知不同:它可以控制目标方法是否真的被调用.
通过调用MethodInvocation.proceed()方法来调用.而且,MethodInterceptor可以控制返回的对象.
你可以返回一个与proceed()方法返回对象完全不同的对象.