Spring AOP四種創建通知(攔截器)類型實例

 
Spring AOP四種創建通知(攔截器)類型實例

1、Spring只支持方法攔截,也就是說,只能在方法的前後進行攔截,而不能在屬性前後進行攔截。
2、Spring支持四種攔截類型:目標方法調用前(before),目標方法調用後(after),目標方法調用前後(around),以及目標方法拋出異常(throw)。
3、前置攔截的類必須實現MethodBeforeAdvice接口,實現其中的before方法。
4、後置攔截的類必須實現AfterReturningAdvice接口,實現其中的afterReturning方法。
5、前後攔截的類必須實現MethodInterceptor接口,實現其中的invoke方法。前後攔截是唯一可以控制目標方法是否被真正調用的攔截類型,也可以控制返回對象。而前置攔截或後置攔截不能控制,它們不能印象目標方法的調用和返回。
但是以上的攔截的問題在於,不能對於特定方法進行攔截,而只能對某個類的全部方法作攔截。所以下面引入了兩個新概念:“切入點”和“引入通知”。
6、”切入點“的定義相當於更加細化地規定了哪些方法被哪些攔截器所攔截,而並非所有的方法都被所有的攔截器所攔截。在ProxyFactoryBean的屬性中,interceptorNames屬性的對象也由攔截(Advice)變成了引入通知(Advisor),正是在Advisor中詳細定義了切入點(PointCut)和攔截(Advice)的對應關係,比如常見的基於名字的切入點匹配(NameMatchMethodPointcutAdvisor類)和基於正則表達式的切入點匹配(RegExpPointcutAdvisor類)。這些切入點都屬於”靜態切入點“,因爲他們只在代理創建的時候被創建一次,而不是每次運行都創建。

下面我們進行實例的開發

首先創建業務接口: 

None.gif500){this.resized=true;this.style.width=500;}" align=top>package AdvisorTest;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
public interface Shopping {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>  
public String buySomething(String type);
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>  
public String buyAnything(String type);
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>  
public void testException();
ExpandedBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>}

None.gif500){this.resized=true;this.style.width=500;}" align=top>

 下面是業務實現類,我們的通知就是以這些實現類作爲切面,在業務方法前後加入我們的通知代碼 

None.gif500){this.resized=true;this.style.width=500;}" align=top>package AdvisorTest;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
public class ShoppingImpl implements Shopping {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
private Customer customer;
ExpandedSubBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedSubBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
public Customer getCustomer() {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        
return customer;
ExpandedSubBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>    }

ExpandedSubBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedSubBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
public void setCustomer(Customer customer) {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        
this.customer = customer;
ExpandedSubBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>    }

ExpandedSubBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedSubBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
public String buySomething(String type) {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        System.out.println(
this.getCustomer().getName()+" bye "+type+" success");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        
return null;
ExpandedSubBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>    }

InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
ExpandedSubBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedSubBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
public String buyAnything(String type) {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>       System.out.println(
this.getCustomer().getName()+" bye "+type+" success");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>       
return null;
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedSubBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>     }

ExpandedSubBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedSubBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
public void testException(){
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        
throw new ClassCastException();
ExpandedSubBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>    }

ExpandedBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>}

None.gif500){this.resized=true;this.style.width=500;}" align=top>

 (1)前置通知

        配置了前置通知的bean,在執行業務方法前,均會執行前置攔截器的before方法

None.gif500){this.resized=true;this.style.width=500;}" align=top>package AdvisorTest;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
import java.lang.reflect.Method;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
import org.springframework.aop.MethodBeforeAdvice;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
//前置通知
ExpandedBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
public class WelcomeAdvice implements MethodBeforeAdvice {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
public void before(Method method, Object[] args, Object obj)
ExpandedSubBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedSubBlock.gif500){this.resized=true;this.style.width=500;}" align=top>            
throws Throwable {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        String type
=(String)args[0];
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        System.out.println(
"Hello welcome to bye "+type);
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedSubBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>    }

InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>}

None.gif500){this.resized=true;this.style.width=500;}" align=top>

 (2)後置通知

配置了前置通知的bean,在執行業務方法前,均會執行前置攔截器的afterReturnning方法
None.gif500){this.resized=true;this.style.width=500;}" align=top>package AdvisorTest;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
import java.lang.reflect.Method;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
import org.springframework.aop.AfterReturningAdvice;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
import org.springframework.aop.MethodBeforeAdvice;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
//後置通知
ExpandedBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
public class ThankYouAdvice implements AfterReturningAdvice {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
public void afterReturning(Object obj, Method method, Object[] arg1,
ExpandedSubBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedSubBlock.gif500){this.resized=true;this.style.width=500;}" align=top>            Object arg2) 
throws Throwable {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>         String type
=(String)arg1[0];
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>         System.out.println(
"Hello Thankyou to bye "+type);
ExpandedSubBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>    }

InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>}

None.gif500){this.resized=true;this.style.width=500;}" align=top>

 (3)環繞通知

配置了前置通知的bean,在執行業務方法前後,均會執行前置攔截器的invoke方法

需要注意的是必須調用目標方法,如不調用,目標方法將不被執行

None.gif500){this.resized=true;this.style.width=500;}" align=top>package AdvisorTest;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
import org.aopalliance.intercept.MethodInterceptor;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
import org.aopalliance.intercept.MethodInvocation;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
public class MethodAdvisor implements MethodInterceptor {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedSubBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedSubBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
public Object invoke(MethodInvocation invocation) throws Throwable {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        String str
=(String)invocation.getArguments()[0];
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        System.out.println(
"this is before"+str+" in MethodInterceptor");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        Object obj
=invocation.proceed(); //調用目標方法,如不調用,目標方法將不被執行
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
        System.out.println("this is after"+str+" in MethodInterceptor");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        
return null;
ExpandedSubBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>    }

InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>}

None.gif500){this.resized=true;this.style.width=500;}" align=top>

 (4)異常通知

ThrowsAdvice是一個標示接口,我們可以在類中定義一個或多個,來捕獲定義異常通知的bean拋出的異常,並在拋出異常前執行相應的方法

public void afterThrowing(Throwable throwa){}或者

public void afterThrowing(Method method,Object[] args,Object target,Throwable throwable){

None.gif500){this.resized=true;this.style.width=500;}" align=top>package AdvisorTest;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
import org.springframework.aop.ThrowsAdvice;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
public  class ExceptionAdvisor implements ThrowsAdvice {
ExpandedSubBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedSubBlock.gif500){this.resized=true;this.style.width=500;}" align=top>  
public void afterThrowing(ClassCastException e){
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>      System.out.println(
"this is from exceptionAdvisor");
ExpandedSubBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>  }

ExpandedBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>}

None.gif500){this.resized=true;this.style.width=500;}" align=top>

 配置文件 

None.gif500){this.resized=true;this.style.width=500;}" align=top><?xml version="1.0" encoding="UTF-8"?>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<beans>
None.gif500){this.resized=true;this.style.width=500;}" align=top> 
<bean id="customer" class="AdvisorTest.Customer">
None.gif500){this.resized=true;this.style.width=500;}" align=top>   
<constructor-arg index="0">
None.gif500){this.resized=true;this.style.width=500;}" align=top>     
<value>gaoxiang</value>
None.gif500){this.resized=true;this.style.width=500;}" align=top>   
</constructor-arg>
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<constructor-arg index="1">
None.gif500){this.resized=true;this.style.width=500;}" align=top>     
<value>26</value>
None.gif500){this.resized=true;this.style.width=500;}" align=top>   
</constructor-arg>
None.gif500){this.resized=true;this.style.width=500;}" align=top> 
</bean>
None.gif500){this.resized=true;this.style.width=500;}" align=top> 
None.gif500){this.resized=true;this.style.width=500;}" align=top> 
<bean id="shoppingImpl" class="AdvisorTest.ShoppingImpl">
None.gif500){this.resized=true;this.style.width=500;}" align=top>   
<property name="customer">
None.gif500){this.resized=true;this.style.width=500;}" align=top>     
<ref local="customer"/>
None.gif500){this.resized=true;this.style.width=500;}" align=top>   
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top> 
</bean>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<!-- 前置通知 -->
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<bean id="welcomeAdvice" class="AdvisorTest.WelcomeAdvice"/>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<bean id="welcomeAdviceShop" class="org.springframework.aop.framework.ProxyFactoryBean">
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
<property name="proxyInterfaces">
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<value>AdvisorTest.Shopping</value>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
<property name="target">
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<ref local="shoppingImpl"/>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
<property name="interceptorNames">
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<list>
None.gif500){this.resized=true;this.style.width=500;}" align=top>      
<value>welcomeAdvice</value>
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
</list>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
None.gif500){this.resized=true;this.style.width=500;}" align=top>
</bean>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<!-- 後置通知 -->
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<bean id="thankyouAdvice" class="AdvisorTest.ThankYouAdvice"/>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<bean id="thankyouAdviceShop" class="org.springframework.aop.framework.ProxyFactoryBean">
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
<property name="proxyInterfaces">
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<value>AdvisorTest.Shopping</value>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
<property name="target">
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<ref local="shoppingImpl"/>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
<property name="interceptorNames">
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<list>
None.gif500){this.resized=true;this.style.width=500;}" align=top>      
<value>thankyouAdvice</value>
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
</list>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
None.gif500){this.resized=true;this.style.width=500;}" align=top>
</bean>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<!-- 環繞通知 -->
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<bean id="methodAdvice" class="AdvisorTest.MethodAdvisor"/>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<bean id="methodAdviceShop" class="org.springframework.aop.framework.ProxyFactoryBean">
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
<property name="proxyInterfaces">
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<value>AdvisorTest.Shopping</value>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
<property name="target">
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<ref local="shoppingImpl"/>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
<property name="interceptorNames">
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<list>
None.gif500){this.resized=true;this.style.width=500;}" align=top>      
<value>methodAdvice</value>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
</list>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
</bean>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<!-- 異常通知 -->
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<bean id="exceptionAdvice" class="AdvisorTest.ExceptionAdvisor"/>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
<bean id="exceptionAdviceShop" class="org.springframework.aop.framework.ProxyFactoryBean">
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
<property name="proxyInterfaces">
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<value>AdvisorTest.Shopping</value>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
<property name="target">
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<ref local="shoppingImpl"/>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
<property name="interceptorNames">
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
<list>
None.gif500){this.resized=true;this.style.width=500;}" align=top>      
<value>exceptionAdvice</value>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
None.gif500){this.resized=true;this.style.width=500;}" align=top>    
</list>
None.gif500){this.resized=true;this.style.width=500;}" align=top>  
</property>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
</bean>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
</beans>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>

 測試代碼:

None.gif500){this.resized=true;this.style.width=500;}" align=top>package AdvisorTest;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
import java.io.File;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
import org.springframework.beans.factory.BeanFactory;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
import org.springframework.beans.factory.xml.XmlBeanFactory;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
import org.springframework.core.io.FileSystemResource;
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
None.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
public class TestAdvisor {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedSubBlockStart.gif500){this.resized=true;this.style.width=500;}" align=top>ContractedSubBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
public static void main(String[] args) {
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        String filePath
=System.getProperty("user.dir")+File.separator+"AdvisorTest"+File.separator+"hello.xml";
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        BeanFactory factory
=new XmlBeanFactory(new FileSystemResource(filePath));
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        Shopping shopping
=null;
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        System.out.println(
"不使用任何通知");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping
=(Shopping)factory.getBean("shoppingImpl");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping.buySomething(
"something");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping.buyAnything(
"anything");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        System.out.println(
"使用前置通知");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping
=(Shopping)factory.getBean("welcomeAdviceShop");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping.buySomething(
"something");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping.buyAnything(
"anything");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        System.out.println(
"使用後置通知");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping
=(Shopping)factory.getBean("thankyouAdviceShop");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping.buySomething(
"something");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping.buyAnything(
"anything");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        System.out.println(
"使用環繞通知");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping
=(Shopping)factory.getBean("methodAdviceShop");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping.buySomething(
"something");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping.buyAnything(
"anything");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        System.out.println(
"使用異常通知");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping
=(Shopping)factory.getBean("exceptionAdviceShop");
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>        shopping.testException();
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>    
InBlock.gif500){this.resized=true;this.style.width=500;}" align=top>
ExpandedSubBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>    }

ExpandedBlockEnd.gif500){this.resized=true;this.style.width=500;}" align=top>}

None.gif500){this.resized=true;this.style.width=500;}" align=top>

 運行結果一目瞭然:

不使用任何通知
gaoxiang bye something success
gaoxiang bye anything success
使用前置通知
Hello welcome to bye something
gaoxiang bye something success
Hello welcome to bye anything
gaoxiang bye anything success
使用後置通知
gaoxiang bye something success
Hello Thankyou to bye something
gaoxiang bye anything success
Hello Thankyou to bye anything
使用環繞通知
this is beforesomething in MethodInterceptor
gaoxiang bye something success
this is aftersomething in MethodInterceptor
this is beforeanything in MethodInterceptor
gaoxiang bye anything success
this is afteranything in MethodInterceptor
使用異常通知
this is from exceptionAdvisor
2007-5-18 22:16:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [E:/項目/SpringInActionStudy/AdvisorTest/hello.xml]
Exception in thread "main" java.lang.ClassCastException
 at AdvisorTest.ShoppingImpl.testException(ShoppingImpl.java:22)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:585)
 at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
 at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:126)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
 at $Proxy0.testException(Unknown Source)
 at AdvisorTest.TestAdvisor.main(TestAdvisor.java:42)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章