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()方法返回對象完全不同的對象.