spring 筆記 (3)-aop

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

發佈了47 篇原創文章 · 獲贊 0 · 訪問量 3097
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章