Spring -- 基于XML的AOP开发

01:Spring – AOP简介
02:Spring – 基于XML的AOP开发
03:Spring – 基于注解的AOP开发

1. 快速入门

快速入门步骤:

在这里插入图片描述


1.1 导入相关依赖

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">
    <parent>
        <artifactId>spring_study</artifactId>
        <groupId>com.tian</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring_aop</artifactId>
    <dependencies>
        <!--        Spring依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--        aop织入依赖-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.4</version>
        </dependency>
        <!--        spring-test依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--        junit单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
</project>

说明:


1.2 创建目标接口和目标接口实现类

在这里插入图片描述

目标接口:

TargetInterface.java

package com.tian.aop;

public interface TargetInterface {
    public void save();
}

目标接口实现类:

Target.java

package com.tian.aop;

public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running.....");
    }
}

1.3 创建切面类

MyAspect.java

package com.tian.aop;

public class MyAspect {
    public void preEnhancement() {
        System.out.println("前置增强..........");
    }

    public void afterEnhancement() {
        System.out.println("后置增强..........");
    }
}

1.4 将目标类和切面类的对象创建权交给spring

在这里插入图片描述
applicationContext.xml

    <!--目标对象-->
    <bean id="target" class="com.tian.aop.Target"/>

    <!--切面对象-->
    <bean id="myAspect" class="com.tian.aop.MyAspect"/>

1.5 在applicationContext.xml中配置织入关系

applicationContext.xml

    <!--配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)-->
    <aop:config>
        <!--声明切面 ref是上面配置的切面类-->
        <aop:aspect ref="myAspect">
        	 <!--before是前置增强 method是切面类的方法 pointcut是切点(里面是具体方法的引用)-->
            <aop:before method="preEnhancement" pointcut="execution(public void com.tian.aop.Target.save())"/>
              <!--after后前置增强 method是切面类的方法 pointcut是切点(里面是具体方法的引用)-->
            <aop:after method="afterEnhancement" pointcut="execution(public void com.tian.aop.Target.save())"/>
        </aop:aspect>
    </aop:config>

1.6 测试代码

在这里插入图片描述

AopTest.java

package test;

import com.tian.aop.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {

    @Autowired
    private TargetInterface target;

    @Test
    public void test1() {
        target.save();
    }
}

运行结果:

在这里插入图片描述


2. XML配置AOP详解


2.1 切点表达式的写法

在这里插入图片描述

常用写法:

// 表示com.tian.aop任意类(一层)的任意方法
execution(* com.tian.aop.*.*(..))
// 表示com.tian.aop任意类(无限层)的任意方法
execution(* com.tian.aop..*.*(..))

所以上面配置文件可以这样写:

在这里插入图片描述


2.2 通知的类型

在这里插入图片描述


2.2.1 环绕通知

前置和后置通知上面已经示例过

切面类中新增一个方法:

MyAspect.java

    //Proceeding JoinPoint: 切点
    public Object aroundEnhancement(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强....");
        Object proceed = pjp.proceed();//切点方法
        System.out.println("环绕后增强....");
        return proceed;
    }

配置环绕通知:

applicationContext.xml

    <!--目标对象-->
    <bean id="target" class="com.tian.aop.Target"/>
    <!--切面对象-->
    <bean id="myAspect" class="com.tian.aop.MyAspect"/>

    <!--配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
            <aop:around method="aroundEnhancement" pointcut="execution(* com.tian.aop..*.*(..))"/>
        </aop:aspect>
    </aop:config>

运行结果(测试代码同上):

在这里插入图片描述


2.2.2 异常抛出通知

切面类中新增一个方法:

MyAspect.java

    public void afterThrowing() {
        System.out.println("异常抛出增强..........");
    }

目标类save方法增加异常:

Target.java

    public void save() {
        int i = 1 / 0;
        System.out.println("save running.....");
    }

配置异常抛出通知:

applicationContext.xml

    <!--目标对象-->
    <bean id="target" class="com.tian.aop.Target"/>
    <!--切面对象-->
    <bean id="myAspect" class="com.tian.aop.MyAspect"/>

    <!--配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
             <aop:after-throwing method="afterThrowing" pointcut="execution(* com.tian.aop..*.*(..))"/>
        </aop:aspect>
    </aop:config>

运行结果(测试代码同上):

在这里插入图片描述


2.2.3 最终通知

切面类中新增一个方法:

    public void finalEnhancement() {
        System.out.println("最终增强..........");
    }

配置异常最终通知:

applicationContext.xml

    <!--目标对象-->
    <bean id="target" class="com.tian.aop.Target"/>

    <!--切面对象-->
    <bean id="myAspect" class="com.tian.aop.MyAspect"/>

    <!--配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
            <aop:after-throwing method="afterThrowing" pointcut="execution(* com.tian.aop..*.*(..))"/>
            <aop:after method="afterEnhancement" pointcut="execution(* com.tian.aop..*.*(..))"/>
        </aop:aspect>
    </aop:config>

运行结果:

在这里插入图片描述


2.3 切点表达式的抽取

在这里插入图片描述

示例:

applicationContext.xml

    <!--目标对象-->
    <bean id="target" class="com.tian.aop.Target"/>

    <!--切面对象-->
    <bean id="myAspect" class="com.tian.aop.MyAspect"/>

    <!--配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
            <aop:pointcut id="myPoint" expression="execution(* com.tian.aop..*.*(..))"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="myPoint"/>
            <aop:after method="afterEnhancement" pointcut-ref="myPoint"/>
        </aop:aspect>
    </aop:config>

说明:

在这里插入图片描述



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeJiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值