日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/5/22 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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(2,?3); ?
  • ????????calculatorProxy.sub(5,?3); ?
  • ????????calculatorProxy.mul(3,?4); ?
  • ????????calculatorProxy.div(6,?1);?
  • 在配置文件中可以看到,使用了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拦截功能的记录的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。