• Spring学习记录(十三)---基于xml文件配置AOP


    上一篇讲了用注解配置AOP,现在讲用xml怎么配置AOP

    其实逻辑是一样的,只是用xml的方法,要把这种逻辑写出来,告诉spring框架去执行。

    例子:这里的例子和上一篇的例子一样。换成xml方式

     1   //方法接口
     2 package com.atguigu.spring.aop.impl;
     3 
     4 public interface Calculator {
     5 
     6     public int add(int i,int j);
     7     public int sub(int i,int j);
     8     public int div(int i,int j);
     9     public int mul(int i,int j);
    10 }
    11   // 方法实现类
    12 package com.atguigu.spring.aop.impl;
    13 
    14 public class CalculatorImpl implements Calculator{
    15 
    16     public int add(int i,int j){
    17         return i+j;
    18         };
    19     public int sub(int i,int j){
    20         return i-j;
    21         };
    22     public int div(int i,int j){
    23         return i/j;
    24         };
    25     public int mul(int i,int j){
    26         return i*j;
    27         }
    28 }

    日志切面:

     1 package com.atguigu.spring.aop.impl;
     2 
     3 import java.util.Arrays;
     4 import java.util.List;
     5 
     6 public class LoggingAspect {
     7        //前置通知日志日志
     8     public void beforedMethod(JoinPoint joinPoint){
     9         String methodName=joinPoint.getSignature().getName();
    10         List<Object> args = Arrays.arrays(joinPoint.getArgs());
    11         System.out.println("The method "+ methodName +"begins: "+args);
    12     }
    13        //返回通知日志
    14     public void afterReturningMethod(Joinpoint joinpoint,Object result){
    15         String methodName=joinPoint.getSignature().getName();
    16         System.out.println("The method "+methodName+" ends with "+result);
    17     }
    18       // 异常通知日志
    19     public void afterThrowingMethod(Joinpoint joinpoint,Exception e){
    20         String methodName=joinPoint.getSignature().getName();
    21         System.out.println("The method "+methodName+" occurs excetion "+e);
    22     }
    23 }

    验证切面:

     1 package com.atguigu.spring.aop.impl;
     2 
     3 import java.util.Arrays;
     4 import java.util.List;
     5 
     6 public class ValidationAspect {
     7     public void beforedMethod(JoinPoint joinPoint){
     8         System.out.println("---->Validation: "+ Arrays.aslist(joinPoint.getArgs));
     9     }
    10 }

    main函数

     1 package com.atguigu.spring.aop.impl;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 public class Main {
     7 
     8     public static void main(String[] args) {
     9 
    10         ApplicationContext ctx=new ClassPathXmlApplicationContext("application.xml");
    11         Calculator cal = (Calculator) ctx.getBean(Calculator.class);
    12         int result=cal.add(1,2);
    13         System.out.println("result: "+result);
    14     }
    15 }

    重点看xml配置:

     1     <!--先配置要使用的bean和切面bean,让spring容器创建对象-->
     2 <!-- 配置bean -->
     3 <bean id="calculatorimpl" class="com.atguigu.spring.aop.impl.Calculatorimpl"></bean>
     4 <!-- 配置切面的bean -->
     5 <bean id="loggingAspect" class="com.atguigu.spring.aop.impl.LoggingAspect"></bean>
     6 <bean id="validationAspect" class="com.atguigu.spring.aop.impl.ValidationAspect"></bean>
     7 
     8    <!-- 配置AOP ,表示出对象中的逻辑-->
     9 <aop:config>
    10     <!-- 配置切点表达式 -->
    11     <aop:pointcut expression="execution(* com.atguigu.spring.aop.impl.CalculatorImpl.*(int, int))" 
    12     id="pointcut"></aop:pointcut>
    13     <!-- 配置切面通知 -->
    14     <aop:aspect ref="validationAspect" order="2">
    15         <aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
    16     </aop:aspect>
    17     <!-- 配置切面通知 -->
    18     <aop:aspect ref="loggingAspect" order="1">
    19         <!--前置通知  -->
    20         <aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
    21         <!--返回通知,注意,方法中有多少个参数,这里就要配置多少个参数,参数值和函数中一致-->
    22         <aop:after-returning method="afterReturningMethod" pointcut-ref="pointcut" returning="result"></aop:after-returning>
    23         <!--异常通知,注意,方法中有多少个参数,这里就要配置多少个参数,参数值和函数中一致-->
    24         <aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="e"></aop:after-throwing>
    25     </aop:aspect>
    26 </aop:config>

    结果和注解配置的一致,但配置比较麻烦,不推荐使用。

  • 相关阅读:
    隔壁小孩都要知道的Drupal配置
    Cobaltstrike、armitage联动
    YxCMS 1.4.7 最新版漏洞分析
    业务逻辑漏洞探索之暴力破解
    刺透内网的HTTP代理
    pwn入门之栈溢出练习
    史上最完整的MySQL注入
    ISG 2018 Web Writeup
    3-4 计算长方形的周长和面积
    【Lucene4.8教程之二】索引
  • 原文地址:https://www.cnblogs.com/ooooevan/p/5823598.html
Copyright © 2020-2023  润新知