基于配置文件的方式来配置 AOP

前端之家收集整理的这篇文章主要介绍了基于配置文件的方式来配置 AOP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

接口类:

  1. package com.spring.aop.xml;
  2.  
  3. public interface ArithmeticCalculator {
  4.  
  5. int add(int i,int j);
  6. int sub(int i,int j);
  7. int mul(int i,int j);
  8. int div(int i,int j);
  9. }

实现类:
  1. package com.spring.aop.xml;
  2.  
  3.  
  4. public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
  5.  
  6. @Override
  7. public int add(int i,int j) {
  8. int result = i + j;
  9. return result;
  10. }
  11.  
  12. @Override
  13. public int sub(int i,int j) {
  14. int result = i - j;
  15. return result;
  16. }
  17.  
  18. @Override
  19. public int mul(int i,int j) {
  20. int result = i * j;
  21. return result;
  22. }
  23.  
  24. @Override
  25. public int div(int i,int j) {
  26. int result = i / j;
  27. return result;
  28. }
  29.  
  30. }
切面类:
  1. package com.spring.aop.xml;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import org.aspectj.lang.JoinPoint;
  6.  
  7. public class VlidationAspect {
  8.  
  9. public void validateArgs(JoinPoint joinPoint){
  10. System.out.println("-->validate:" + Arrays.asList(joinPoint.getArgs()));
  11. }
  12. }

切面类2:
  1. package com.spring.aop.xml;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import org.aspectj.lang.JoinPoint;
  6. import org.aspectj.lang.ProceedingJoinPoint;
  7. import org.aspectj.lang.annotation.Around;
  8.  
  9. public class LoggingAspect {
  10. public void beforeMethod(JoinPoint joinPoint){
  11. String methodName = joinPoint.getSignature().getName();
  12. Object [] args = joinPoint.getArgs();
  13. System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
  14. }
  15. public void afterMethod(JoinPoint joinPoint){
  16. String methodName = joinPoint.getSignature().getName();
  17. System.out.println("The method " + methodName + " ends");
  18. }
  19. public void afterReturning(JoinPoint joinPoint,Object result){
  20. String methodName = joinPoint.getSignature().getName();
  21. System.out.println("The method " + methodName + " ends with " + result);
  22. }
  23. public void afterThrowing(JoinPoint joinPoint,Exception e){
  24. String methodName = joinPoint.getSignature().getName();
  25. System.out.println("The method " + methodName + " occurs excetion:" + e);
  26. }
  27. @Around("execution(public int com.spring.aop.ArithmeticCalculator.*(..))")
  28. public Object aroundMethod(ProceedingJoinPoint pjd){
  29. Object result = null;
  30. String methodName = pjd.getSignature().getName();
  31. try {
  32. //前置通知
  33. System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
  34. //执行目标方法
  35. result = pjd.proceed();
  36. //返回通知
  37. System.out.println("The method " + methodName + " ends with " + result);
  38. } catch (Throwable e) {
  39. //异常通知
  40. System.out.println("The method " + methodName + " occurs exception:" + e);
  41. throw new RuntimeException(e);
  42. }
  43. //后置通知
  44. System.out.println("The method " + methodName + " ends");
  45. return result;
  46. }
  47. }

测试类:
  1. package com.spring.aop.xml;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class Main {
  7. public static void main(String[] args) {
  8. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
  9. ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
  10. System.out.println(arithmeticCalculator.getClass().getName());
  11. int result = arithmeticCalculator.add(1,2);
  12. System.out.println("result:" + result);
  13. result = arithmeticCalculator.div(1000,0);
  14. System.out.println("result:" + result);
  15. }
  16. }

配置文件xml:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
  7.  
  8. <!-- 配置 bean -->
  9. <bean id="arithmeticCalculator"
  10. class="comspring.aop.xml.ArithmeticCalculatorImpl"></bean>
  11.  
  12. <!-- 配置切面的 bean. -->
  13. <bean id="loggingAspect"
  14. class="com.spring.aop.xml.LoggingAspect"></bean>
  15.  
  16. <bean id="vlidationAspect"
  17. class="com.spring.aop.xml.VlidationAspect"></bean>
  18.  
  19. <!-- 配置 AOP -->
  20. <aop:config>
  21. <!-- 配置切点表达式 -->
  22. <aop:pointcut expression="execution(* com.spring.aop.xml.ArithmeticCalculator.*(int,int))"
  23. id="pointcut"/>
  24. <!-- 配置切面及通知 -->
  25. <aop:aspect ref="loggingAspect" order="2">
  26. <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
  27. <aop:after method="afterMethod" pointcut-ref="pointcut"/>
  28. <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
  29. <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
  30. <!--
  31. <aop:around method="aroundMethod" pointcut-ref="pointcut"/>
  32. -->
  33. </aop:aspect>
  34. <aop:aspect ref="vlidationAspect" order="1">
  35. <aop:before method="validateArgs" pointcut-ref="pointcut"/>
  36. </aop:aspect>
  37. </aop:config>
  38.  
  39. </beans>

猜你在找的XML相关文章