日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/5/22 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring中关于aop拦截功能的记录 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

java寫了個接口并寫了個實(shí)現(xiàn)類:

?

  • 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"?/>?
  • 攔截器的實(shí)現(xiàn)類為:

    ?

  • package?myspring.aop; ?
  • ?
  • import?org.aopalliance.intercept.MethodInterceptor; ?
  • import?org.aopalliance.intercept.MethodInvocation; ?
  • ?
  • public?class?LogAroundAdvice?implements?MethodInterceptor{ ?
  • ?
  • ????//MethodInvocation相當(dāng)于Method的包裝類 ?
  • ????public?Object?invoke(MethodInvocation?methodInvocation)?throws?Throwable?{ ?
  • ????????MyLogger?logger?=?new?MyLogger(); ?
  • ???????? ?
  • ????????try{ ?
  • ????????????logger.log("before:?"+methodInvocation.getMethod().getName()+ ?
  • ????????????????????????????????",????args"+methodInvocation.getArguments()); ?
  • ???????????? ?
  • ????????????//須執(zhí)行目標(biāo)方法! ?
  • ????????????Object?result?=?methodInvocation.proceed(); ?
  • ???????????? ?
  • ????????????logger.log("after:?"+methodInvocation.getMethod().getName()); ?
  • ???????????? ?
  • ????????????//注意返回目標(biāo)方法的返回值 ?
  • ????????????return?result; ?
  • ????????} ?
  • ????????catch(Exception?e) ?
  • ????????{ ?
  • ????????????logger.log("Exception:?"+e.getMessage()); ?
  • ????????????throw?e; ?
  • ????????} ?
  • ???????? ?
  • ????} ?
  • ?
  • } ?
  • 這個類實(shí)現(xiàn)了MethodInterceptor這個接口,表示在調(diào)用目標(biāo)方法之前與之后都執(zhí)行攔截。

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

    ?

  • <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>?
  • 以上的方法可以攔截目標(biāo)類中的所有方法,如果只攔截指定方法就不能用這個了。

    配置文件要換一下

    ?

  • <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>?
  • ?

    轉(zhuǎn)載于:https://blog.51cto.com/4045060/780566

    總結(jié)

    以上是生活随笔為你收集整理的spring中关于aop拦截功能的记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。