Spring中基于xml的AOP实现详解

 更新时间:2023年09月13日 09:16:40   作者:蕾峰  
这篇文章主要介绍了Spring中基于xml的AOP实现详解,基于xml与基于注解的AOP本质上是非常相似的,都是需要封装横切关注点,封装到切面中,然后把横切关注点封装为一个方法,再把该方法设置为当前的一个通知,再通过切入点表达式定位到横切点就可以了,需要的朋友可以参考下

基于xml的AOP实现

基于xml与基于注解的AOP本质上是非常相似的,都是需要封装横切关注点,封装到切面中,然后把横切关注点封装为一个方法,再把该方法设置为当前的一个通知,再通过切入点表达式定位到横切点就可以了,思路是非常相似的。

我们的接口代码如下所示:

package com.rgf.spring.aop.annotation.xml;
import org.springframework.stereotype.Component;
@Component
public interface Calculator {
    int add(int i,int j);
    int sub(int i,int j);
    int mul(int i,int j);
    int div(int i,int j);
}

我们的实现类如下所示:

package com.rgf.spring.aop.annotation.xml;
import org.springframework.stereotype.Component;
/**
 *
 */
@Component
public class CalculatorImpl implements Calculator {
    @Override
    public int add(int i, int j) {
        int result=i+j;
        System.out.println("方法内部,result:"+result);
        return result;
    }
    @Override
    public int sub(int i, int j) {
        int result=i-j;
        System.out.println("方法内部,result:"+result);
        return  result;
    }
    @Override
    public int mul(int i, int j) {
       int result=i*j;
       System.out.println("方法内部,result:"+result);
       return  result;
    }
    @Override
    public int div(int i, int j) {
      int result=i/j;
      System.out.println("方法内部,result:"+result);
      return  result;
    }
}

我们封装的切面方法如下所示:

package com.rgf.spring.aop.annotation.xml;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class LoggerAspect {
     public  void  beforeAdviceMethod(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect,方法:"+signature.getName()+",参数:"+ Arrays.toString(args));
    }
    public  void afterAdviceMethod(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect,方法:"+signature.getName()+",执行完毕");
    }
    public  void afterReturningAdviceMethod(JoinPoint joinPoint,Object result){
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect,方法:"+signature.getName()+",结果:"+result);
    }
    public  void afterThrowingAdviceMethod(JoinPoint joinPoint,Throwable ex){
        Signature signature = joinPoint.getSignature();
        System.out.println("LoggerAspect,方法:"+signature.getName()+",异常:"+ex);
    }
    public  Object  aroundAdviceMethod(ProceedingJoinPoint joinPoint){
       Object result=null;
        try {
            System.out.println("环绕通知-->前置通知");
            //表示目标对象方法的执行
            result = joinPoint.proceed();
            System.out.println("环绕通知-->返回通知");
        } catch (Throwable throwable) {
           throwable.printStackTrace();
            System.out.println("环绕通知-->异常通知");
        }finally {
            System.out.println("环绕通知-->后置通知");
        }
      return result;
    }
}

我们封装的切面如下所示:

package com.rgf.spring.aop.annotation.xml;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
 * 切面的优先级:
 * 可以通过@Order注解的value属性设置优先级,默认值Integer的最大值
 * @Order注解的value属性值越小,优先级越高
 *
 */
@Component
public class ValidateAspect {
    public void beforeMethod(){
        System.out.println("ValidateAspect-->前置通知");
    }
}

我们的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描组件-->
    <context:component-scan base-package="com.rgf.spring.aop.annotation.xml"></context:component-scan>
   <!-- <cop:aspectj-autoproxy:开启基于注解的aop-->
    <aop:config>
        <!--设置一个公共的切入点表达式-->
        <aop:pointcut id="pointcut" expression="execution(* com.rgf.spring.aop.annotation.xml.CalculatorImpl.*(..))"/>
        <!--aop:aspect:将IOC容器中的某个bean(某个组件)设置为切面,通过ref引用某一个besn的id,就可以将当前这一个bean来设置为一个切面-->
        <!--aop:advisor:设置当前的通知-->
        <!--aop:pointcut:设置切入点表达式-->
        <aop:aspect ref="loggerAspect">
            <aop:before method="beforeAdviceMethod" pointcut-ref="pointcut"></aop:before>
            <aop:after  method="afterAdviceMethod" pointcut-ref="pointcut"></aop:after>
            <aop:after-returning method="afterReturningAdviceMethod" returning="result" pointcut-ref="pointcut"></aop:after-returning>
            <aop:after-throwing method="afterThrowingAdviceMethod" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
            <aop:around method="aroundAdviceMethod" pointcut-ref="pointcut"></aop:around>
        </aop:aspect>
        <!--order进行设置他的优先级.设置的值越小,优先级越高-->
        <aop:aspect ref="validateAspect" order="1">
            <aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

我们进行测试如下所示:

package com.rgf.spring.test;
import com.rgf.spring.aop.annotation.xml.Calculator;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AOPByXMLTest {
    @Test
    public  void  testAOPByXML(){
        ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-xml.xml");
        Calculator calculator = ioc.getBean(Calculator.class);
        calculator.add(1,1);
    }
}

我们进行运行之后如下所示:

到此这篇关于Spring中基于xml的AOP实现详解的文章就介绍到这了,更多相关基于xml的AOP实现内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用JavaMail发送邮件保证成功的方法

    使用JavaMail发送邮件保证成功的方法

    JavaMail是利用现有的邮件账户发送邮件的工具,使用过JavaMail的api发送邮件的人可能会有这样一个疑惑:我如何知道我调用该api发送的邮件是否成功呢?那么通过下面这篇文章大家一起来看看使用JavaMail保证邮件发送成功的方法,有需要的朋友们可以参考借鉴。
    2016-11-11
  • mybatis类型转换器如何实现数据加解密

    mybatis类型转换器如何实现数据加解密

    这篇文章主要介绍了mybatis类型转换器如何实现数据加解密,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • java实战技巧之if-else代码优化技巧大全

    java实战技巧之if-else代码优化技巧大全

    代码中如果if-else比较多,阅读起来比较困难,维护起来也比较困难,很容易出bug,下面这篇文章主要给大家介绍了关于java实战技巧之if-else代码优化技巧的相关资料,需要的朋友可以参考下
    2022-02-02
  • JAVA文件扫描(递归)的实例代码

    JAVA文件扫描(递归)的实例代码

    这篇文章主要介绍了JAVA文件扫描(递归)的实例代码 ,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-06-06
  • Java中Spring Boot+Socket实现与html页面的长连接实例详解

    Java中Spring Boot+Socket实现与html页面的长连接实例详解

    这篇文章主要介绍了Java中Spring Boot+Socket实现与html页面的长连接实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Java基础知识精通块作用域与条件及switch语句

    Java基础知识精通块作用域与条件及switch语句

    块(block,即复合语句)是指由若干条 Java 语句组成的语句,并由一对大括号括起来。块确定了变量的作用域。一个块可以嵌套在另一个块中;条件语句、switch语句是我们常见会用到的结构,感兴趣的朋友来看看吧
    2022-04-04
  • Java语言实现简单FTP软件 FTP连接管理模块实现(8)

    Java语言实现简单FTP软件 FTP连接管理模块实现(8)

    这篇文章主要为大家详细介绍了Java语言实现简单FTP软件,FTP连接管理模块的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • 解析阿里GTS开源版本fescar分布式事务

    解析阿里GTS开源版本fescar分布式事务

    这篇文章主要为大家介绍解析阿里GTS开源版本fescar分布式事务的原理及使用说明,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多进步
    2022-02-02
  • 基于Column注解的columnDefinition用法

    基于Column注解的columnDefinition用法

    这篇文章主要介绍了Column注解的columnDefinition用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Spring Boot超详细分析启动流程

    Spring Boot超详细分析启动流程

    SpringBoot是Spring开源组织下的子项目,是Spring组件一站式解决方案,主要是简化了使用Spring的难度,简省了繁重的配置,提供了各种启动器,开发者能快速上手,这篇文章主要给大家介绍了关于Spring Boot启动流程知识点的相关资料,需要的朋友可以参考下
    2022-07-07

最新评论