生活随笔
收集整理的这篇文章主要介绍了
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{ 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"> <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> 以上的方法可以拦截目标类中的所有方法,如果只拦截指定方法就不能用这个了。
配置文件要换一下
<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>
转载于:https://blog.51cto.com/4045060/780566
总结
以上是生活随笔为你收集整理的spring中关于aop拦截功能的记录的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。