欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

spring中关于aop拦截功能的记录

发布时间:2025/5/22 编程问答 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 spring中关于aop拦截功能的记录 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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

 

  • package myspring.calculator;  
  •  
  • public interface IArithmeticCalculator {  
  •  
  •     public double add(double a, double b);  
  •     public double sub(double a, double b);  
  •     public double mul(double a, double b);  
  •     public double div(double a, double b);  
  • }  
  •  

  • package myspring.calculator;  
  •  
  • import org.apache.commons.logging.Log;  
  • import org.apache.commons.logging.LogFactory;  
  •  
  • public class ArithmeticCalculatorImp implements IArithmeticCalculator {  
  •       
  •     public double add(double a, double b) {  
  •         double result = a + b;  
  •         System.out.println(a + " + " + b + " = " + result);  
  •         return result;  
  •     }  
  •  
  •     public double sub(double a, double b) {  
  •         double result = a - b;  
  •         System.out.println(a + " - " + b + " = " + result);  
  •         return result;  
  •     }  
  •  
  •     public double mul(double a, double b) {  
  •         double result = a * b;  
  •         System.out.println(a + " * " + b + " = " + result);  
  •         return result;  
  •     }  
  •  
  •     public double div(double a, double b) {  
  •         if (b == 0) {  
  •             throw new IllegalArgumentException("Division by zero");  
  •         }  
  •         double result = a / b;  
  •         System.out.println(a + " / " + b + " = " + result);  
  •         return result;  
  •     }  
  •       
  •       
  • }  
  •  

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

     

  • <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" /> 
  • <bean id="arithmeticCalculatorProxy" 
  •                 class="org.springframework.aop.framework.ProxyFactoryBean"> 
  •         <property name="target" ref="arithmeticCalculator"></property> 
  •         <property name="interceptorNames"><!--定义使用何种方式拦截--> 
  •             <list> 
  •                 <!-- <value>logBeforeAdvice</value> 
  •                 <value>logAfterReturning</value> 
  •                 <value>logThrowsAdvice</value> --> 
  •                 <value>logAroundAdvice</value> 
  •             </list> 
  •         </property> 
  •     </bean> 
  • 在用到接口里方法的地方加载配置文件并获得代理类:

     

  • ApplicationContext ac = new ClassPathXmlApplicationContext("aop-base.xml");  
  •         IArithmeticCalculator calculatorProxy =   
  •                             (IArithmeticCalculator)ac.getBean("arithmeticCalculatorProxy");  
  •                           
  •           
  •         calculatorProxy.add(23);  
  •         calculatorProxy.sub(53);  
  •         calculatorProxy.mul(34);  
  •         calculatorProxy.div(61); 
  • 在配置文件中可以看到,使用了logAroundAdvice这个拦截器(我也不知道是不是叫拦截器)

     

  • 配置文件中还要配置拦截器:  
  • <bean id="logAroundAdvice" class="myspring.aop.LogAroundAdvice" /> 
  • 拦截器的实现类为:

     

  • package myspring.aop;  
  •  
  • import org.aopalliance.intercept.MethodInterceptor;  
  • import org.aopalliance.intercept.MethodInvocation;  
  •  
  • public class LogAroundAdvice implements MethodInterceptor{  
  •  
  •     //MethodInvocation相当于Method的包装类  
  •     public Object invoke(MethodInvocation methodInvocation) throws Throwable {  
  •         MyLogger logger = new MyLogger();  
  •           
  •         try{  
  •             logger.log("before: "+methodInvocation.getMethod().getName()+  
  •                                 ",    args"+methodInvocation.getArguments());  
  •               
  •             //须执行目标方法!  
  •             Object result = methodInvocation.proceed();  
  •               
  •             logger.log("after: "+methodInvocation.getMethod().getName());  
  •               
  •             //注意返回目标方法的返回值  
  •             return result;  
  •         }  
  •         catch(Exception e)  
  •         {  
  •             logger.log("Exception: "+e.getMessage());  
  •             throw e;  
  •         }  
  •           
  •     }  
  •  
  • }  
  • 这个类实现了MethodInterceptor这个接口,表示在调用目标方法之前与之后都执行拦截。

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

     

  • <beans xmlns="http://www.springframework.org/schema/beans" 
  •     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  •     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  •         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  •  
  •     <!-- Target --> 
  •     <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" /> 
  •  
  •     <!-- Advice --> 
  •     <bean id="logBeforeAdvice" class="myspring.aop.LogBeforeAdvice" /> 
  •     <bean id="logAfterReturning" class="myspring.aop.LogAfterReturningAdvice" /> 
  •     <bean id="logThrowsAdvice" class="myspring.aop.logThrowsAdvice" /> 
  •     <bean id="logAroundAdvice" class="myspring.aop.LogAroundAdvice" /> 
  •       
  •     <!-- Proxy --> 
  •     <bean id="arithmeticCalculatorProxy" 
  •                 class="org.springframework.aop.framework.ProxyFactoryBean"> 
  •         <property name="target" ref="arithmeticCalculator"></property> 
  •         <property name="interceptorNames"> 
  •             <list> 
  •                 <!-- <value>logBeforeAdvice</value> 
  •                 <value>logAfterReturning</value> 
  •                 <value>logThrowsAdvice</value> --> 
  •                 <value>logAroundAdvice</value> 
  •             </list> 
  •         </property> 
  •     </bean> 
  • </beans> 
  • 以上的方法可以拦截目标类中的所有方法,如果只拦截指定方法就不能用这个了。

    配置文件要换一下

     

  • <bean id="arithmeticCalculatorProxy" 
  •                 class="org.springframework.aop.framework.ProxyFactoryBean"> 
  •         <property name="target" ref="arithmeticCalculator"></property> 
  •         <property name="interceptorNames"> 
  •             <list> 
  •                 <value>nameMatchAdvisor</value>                             </list> 
  •         </property> 
  •     </bean> 
  • <bean id="nameMatchAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> 
  •         <property name="mappedNames"> 
  •             <list> 
  •                 <value>add</value><!--只拦截这两个方法--> 
  •                 <value>sub</value> 
  •             </list> 
  •         </property> 
  •         <property name="advice" ref="logAroundAdvice" /><!--按照这个拦截器的方式拦截那两个方法--> 
  •  
  •     </bean> 
  • 以上拦截方式中的完整配置文件如下:

     

  • <beans xmlns="http://www.springframework.org/schema/beans" 
  •     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  •     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  •         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  •  
  •     <!-- Target --> 
  •     <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" /> 
  •  
  •     <!-- Advice --> 
  •     <bean id="logBeforeAdvice" class="myspring.aop.LogBeforeAdvice" /> 
  •     <bean id="logAfterReturning" class="myspring.aop.LogAfterReturningAdvice" /> 
  •     <bean id="logThrowsAdvice" class="myspring.aop.logThrowsAdvice" /> 
  •     <bean id="logAroundAdvice" class="myspring.aop.LogAroundAdvice" /> 
  •       
  •       
  •     <bean id="nameMatchAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> 
  •         <property name="mappedNames"> 
  •             <list> 
  •                 <value>add</value> 
  •                 <value>sub</value> 
  •             </list> 
  •         </property> 
  •         <property name="advice" ref="logAroundAdvice" /> 
  •     </bean> 
  •  
  •     <bean id="arithmeticCalculatorProxy" 
  •                 class="org.springframework.aop.framework.ProxyFactoryBean"> 
  •         <property name="target" ref="arithmeticCalculator"></property> 
  •         <property name="interceptorNames"> 
  •             <list> 
  •                  <value>nameMatchAdvisor</value> 
  •             </list> 
  •         </property> 
  •     </bean> 
  • </beans> 
  •  

    转载于:https://blog.51cto.com/4045060/780566

    总结

    以上是生活随笔为你收集整理的spring中关于aop拦截功能的记录的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。