生活随笔
收集整理的這篇文章主要介紹了
spring中关于aop拦截功能的记录
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
java寫了個(gè)接口并寫了個(gè)實(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這個(gè)攔截器(我也不知道是不是叫攔截器)
?
配置文件中還要配置攔截器: ?<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{ ???????????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; ?????????} ????????? ?????} ??} ? 這個(gè)類實(shí)現(xiàn)了MethodInterceptor這個(gè)接口,表示在調(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">???????????<bean?id="arithmeticCalculator"?class="myspring.calculator.ArithmeticCalculatorImp"?/>???????????<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="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)類中的所有方法,如果只攔截指定方法就不能用這個(gè)了。
配置文件要換一下
?
<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">???????????<bean?id="arithmeticCalculator"?class="myspring.calculator.ArithmeticCalculatorImp"?/>???????????<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拦截功能的记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。