spring的基于xml的AOP的案例

本文详细介绍了如何在Spring中使用XML配置实现AOP,包括环境配置、代理和被代理类创建、各种通知(前置、后置、异常、最终)的bean.xml配置。在遇到环绕通知执行问题时,解释了 ProceedingJoinPoint 接口的作用以及如何解决切入点方法未执行的问题。最后展示了配置的实际效果。
摘要由CSDN通过智能技术生成

spring的基于xml的AOP的案例

基本的环境配置

首先建立maven工程,在pom.xml中导入

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springxml-proxy-config</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
    </dependencies>
</project>

包的展示

建立代理和被代理类

被代理类的接口

package test.service;

public interface service {
    public void  getall();


}

被代理的类

package test.service.impl;

import test.service.service;

public class serviceimpl implements service {


    public void getall() {
        System.out.println("需要代理增强的函数");
    }
}

需要在切面执行的通知的类的方法

package test.service.util;

import org.aspectj.lang.ProceedingJoinPoint;

public class publiccode {
    public void Addcode(){
        System.out.println("ADDcode函数增强了");
    }
    public void Addcode1(){
        System.out.println("ADDcode函数增强了1");
    }
   }

环绕通知的函数,比较特殊,所以特意提出来,是上个类的函数

环绕通知
问题:
当我们配置了环绕通知之后,切入点方法没有执行,而通知方法执行了。
分析:
通过对比动态代理中的环绕通知代码,发现动态代理的环绕通知有明确的切入点方法调用,而我们的代码中没有。
解决:
Spring框架为我们提供了一个接口:ProceedingJoinPoint。该接口有一个方法proceed(),此方法就相当于明确调用切入点方法。
该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用。
spring中的环绕通知:
它是spring框架为我们提供的一种可以在代码中手动控制增强方法何时执行的方式。

  public void   Addcode2(ProceedingJoinPoint pjp)  {
        Object rtValue=null;

        try {
            System.out.println("前置");
            Object []args=pjp.getArgs();
            System.out.println("环绕通知执行了");
            rtValue=pjp.proceed(args);
            System.out.println("后置");
       }catch (Throwable e){
           System.out.println("异常");
           throw new RuntimeException(e);
       }finally {
           System.out.println("最终");
       }

前置、后置、异常、最终通知的bean.xml的配置

配置切入点表达式 id属性用于指定表达式的唯一标识。expression属性用于指定表达式内容
此标签写在aop:aspect标签内部只能当前切面使用。
它还可以写在aop:aspect外面,此时就变成了所有切面可用

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="service" class="test.service.impl.serviceimpl"></bean>

    <bean id="log" class="test.service.util.publiccode"></bean>

    <aop:config>
        <aop:aspect id="logAdvice" ref="log">

            <aop:pointcut id="servicecut" expression="execution(public void test.service.impl.serviceimpl.getall())"/>
            <aop:before method="Addcode" pointcut-ref="servicecut"/>
            <aop:after method="Addcode"   pointcut-ref="servicecut"/>
            <aop:after-returning method="Addcode1" pointcut="execution(* *.*.*.*.getall())"/>
            <aop:around method="Addcode2" pointcut-ref="servicecut"/>
        </aop:aspect>

    </aop:config>
</beans>

结果展示

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值