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.                 <!-- <value>logBeforeAdvice</value> 
  8.                 <value>logAfterReturning</value> 
  9.                 <value>logThrowsAdvice</value> --> 
  10.                 <value>logAroundAdvice</value> 
  11.             </list> 
  12.         </property> 
  13.     </bean> 

在用到接口里方法的地方加载配置文件并获得代理类:

 

 
  
  1. ApplicationContext ac = new ClassPathXmlApplicationContext("aop-base.xml");  
  2.         IArithmeticCalculator calculatorProxy =   
  3.                             (IArithmeticCalculator)ac.getBean("arithmeticCalculatorProxy");  
  4.                           
  5.           
  6.         calculatorProxy.add(23);  
  7.         calculatorProxy.sub(53);  
  8.         calculatorProxy.mul(34);  
  9.         calculatorProxy.div(61); 

在配置文件中可以看到,使用了logAroundAdvice这个拦截器(我也不知道是不是叫拦截器)

 

 
  
  1. 配置文件中还要配置拦截器:  
  2. <bean id="logAroundAdvice" class="myspring.aop.LogAroundAdvice" /> 

拦截器的实现类为:

 

 
  
  1. package myspring.aop;  
  2.  
  3. import org.aopalliance.intercept.MethodInterceptor;  
  4. import org.aopalliance.intercept.MethodInvocation;  
  5.  
  6. public class LogAroundAdvice implements MethodInterceptor{  
  7.  
  8.     //MethodInvocation相当于Method的包装类  
  9.     public Object invoke(MethodInvocation methodInvocation) throws Throwable {  
  10.         MyLogger logger = new MyLogger();  
  11.           
  12.         try{  
  13.             logger.log("before: "+methodInvocation.getMethod().getName()+  
  14.                                 ",    args"+methodInvocation.getArguments());  
  15.               
  16.             //须执行目标方法!  
  17.             Object result = methodInvocation.proceed();  
  18.               
  19.             logger.log("after: "+methodInvocation.getMethod().getName());  
  20.               
  21.             //注意返回目标方法的返回值  
  22.             return result;  
  23.         }  
  24.         catch(Exception e)  
  25.         {  
  26.             logger.log("Exception: "+e.getMessage());  
  27.             throw e;  
  28.         }  
  29.           
  30.     }  
  31.  
  32. }  

这个类实现了MethodInterceptor这个接口,表示在调用目标方法之前与之后都执行拦截。

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

 

 
  
  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.     <!-- Target --> 
  7.     <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" /> 
  8.  
  9.     <!-- Advice --> 
  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.     <!-- Proxy --> 
  16.     <bean id="arithmeticCalculatorProxy" 
  17.                 class="org.springframework.aop.framework.ProxyFactoryBean"> 
  18.         <property name="target" ref="arithmeticCalculator"></property> 
  19.         <property name="interceptorNames"> 
  20.             <list> 
  21.                 <!-- <value>logBeforeAdvice</value> 
  22.                 <value>logAfterReturning</value> 
  23.                 <value>logThrowsAdvice</value> --> 
  24.                 <value>logAroundAdvice</value> 
  25.             </list> 
  26.         </property> 
  27.     </bean> 
  28. </beans> 

以上的方法可以拦截目标类中的所有方法,如果只拦截指定方法就不能用这个了。

配置文件要换一下

 

 
  
  1. <bean id="arithmeticCalculatorProxy" 
  2.                 class="org.springframework.aop.framework.ProxyFactoryBean"> 
  3.         <property name="target" ref="arithmeticCalculator"></property> 
  4.         <property name="interceptorNames"> 
  5.             <list> 
  6.                 <value>nameMatchAdvisor</value>                             </list> 
  7.         </property> 
  8.     </bean> 
  9. <bean id="nameMatchAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> 
  10.         <property name="mappedNames"> 
  11.             <list> 
  12.                 <value>add</value><!--只拦截这两个方法--> 
  13.                 <value>sub</value> 
  14.             </list> 
  15.         </property> 
  16.         <property name="advice" ref="logAroundAdvice" /><!--按照这个拦截器的方式拦截那两个方法--> 
  17.  
  18.     </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.     <!-- Target --> 
  7.     <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" /> 
  8.  
  9.     <!-- Advice --> 
  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>add</value> 
  20.                 <value>sub</value> 
  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>nameMatchAdvisor</value> 
  32.             </list> 
  33.         </property> 
  34.     </bean> 
  35. </beans>