java写了个接口并写了个实现类:

 

   
   
   
   
  1. package myspring.calculator;  
  2.  
  3. public interface IArithmeticCalculator {  
  4.  
  5.     public double add(double a, double b);  
  6.     public double sub(double a, double b);  
  7.     public double mul(double a, double b);  
  8.     public double div(double a, double b);  
  9. }  

 

   
   
   
   
  1. package myspring.calculator;  
  2.  
  3. import org.apache.commons.logging.Log;  
  4. import org.apache.commons.logging.LogFactory;  
  5.  
  6. public class ArithmeticCalculatorImp implements IArithmeticCalculator {  
  7.       
  8.     public double add(double a, double b) {  
  9.         double result = a + b;  
  10.         System.out.println(a + " + " + b + " = " + result);  
  11.         return result;  
  12.     }  
  13.  
  14.     public double sub(double a, double b) {  
  15.         double result = a - b;  
  16.         System.out.println(a + " - " + b + " = " + result);  
  17.         return result;  
  18.     }  
  19.  
  20.     public double mul(double a, double b) {  
  21.         double result = a * b;  
  22.         System.out.println(a + " * " + b + " = " + result);  
  23.         return result;  
  24.     }  
  25.  
  26.     public double div(double a, double b) {  
  27.         if (b == 0) {  
  28.             throw new IllegalArgumentException("Division by zero");  
  29.         }  
  30.         double result = a / b;  
  31.         System.out.println(a + " / " + b + " = " + result);  
  32.         return result;  
  33.     }  
  34.       
  35.       
  36. }  

 

在spring配置文件aop-base.xml中配置好

 

   
   
   
   
  1. <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" /> 
  2. <bean id="arithmeticCalculatorProxy" 
  3.                 class="org.springframework.aop.framework.ProxyFactoryBean"> 
  4.         <property name="target" ref="arithmeticCalculator">property> 
  5.         <property name="interceptorNames"> 
  6.             <list> 
  7.                  
  8.     <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" /> 
  9.  
  10.      
  11.     <bean id="logBeforeAdvice" class="myspring.aop.LogBeforeAdvice" /> 
  12.     <bean id="logAfterReturning" class="myspring.aop.LogAfterReturningAdvice" /> 
  13.     <bean id="logThrowsAdvice" class="myspring.aop.logThrowsAdvice" /> 
  14.     <bean id="logAroundAdvice" class="myspring.aop.LogAroundAdvice" /> 
  15.       
  16.      
  17.     <bean id="arithmeticCalculatorProxy" 
  18.                 class="org.springframework.aop.framework.ProxyFactoryBean"> 
  19.         <property name="target" ref="arithmeticCalculator">property> 
  20.         <property name="interceptorNames"> 
  21.             <list> 
  22.                  
  23.                 <value>subvalue> 
  24.             list> 
  25.         property> 
  26.         <property name="advice" ref="logAroundAdvice" /> 
  27.  
  28.     bean> 

以上拦截方式中的完整配置文件如下:

 

   
   
   
   
  1. <beans xmlns="http://www.springframework.org/schema/beans" 
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  4.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  5.  
  6.      
  7.     <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" /> 
  8.  
  9.      
  10.     <bean id="logBeforeAdvice" class="myspring.aop.LogBeforeAdvice" /> 
  11.     <bean id="logAfterReturning" class="myspring.aop.LogAfterReturningAdvice" /> 
  12.     <bean id="logThrowsAdvice" class="myspring.aop.logThrowsAdvice" /> 
  13.     <bean id="logAroundAdvice" class="myspring.aop.LogAroundAdvice" /> 
  14.       
  15.       
  16.     <bean id="nameMatchAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> 
  17.         <property name="mappedNames"> 
  18.             <list> 
  19.                 <value>addvalue> 
  20.                 <value>subvalue> 
  21.             list> 
  22.         property> 
  23.         <property name="advice" ref="logAroundAdvice" /> 
  24.     bean> 
  25.  
  26.     <bean id="arithmeticCalculatorProxy" 
  27.                 class="org.springframework.aop.framework.ProxyFactoryBean"> 
  28.         <property name="target" ref="arithmeticCalculator">property> 
  29.         <property name="interceptorNames"> 
  30.             <list> 
  31.                  <value>nameMatchAdvisorvalue> 
  32.             list> 
  33.         property> 
  34.     bean> 
  35. beans>