Spring > AOP切面编程,XML文件配置

1 > maven工程pom依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.7</version>
    </dependency>
</dependencies>

2 > 需要用到的类

2.1 > 接口及其实现类

在这里插入图片描述

2.2 > 通知类

package com.xx01.advice;

import org.aspectj.lang.ProceedingJoinPoint;

public class Logger {
    
    
    public void beforeAdvice() {
    
    
        // 前置通知
        System.out.println("This is : " + Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    public void afterRunningAdvice() {
    
    
        // 后置通知
        System.out.println("This is : " + Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    public void exceptionAdvice() {
    
    
        // 异常通知
        System.out.println("This is : " + Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    public void afterAdvice() {
    
    
        // 最终通知
        System.out.println("This is : " + Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    public Object roundAdvice(ProceedingJoinPoint point) {
    
    
        // 设置的返回对象
        Object returnValue = null;

        // 得到方法执行所需要的参数
        Object[] args = point.getArgs();
        try {
    
    
            // 前置通知
            System.out.println("This is : " + Thread.currentThread().getStackTrace()[1].getMethodName());
            //  明确调用Dao层的方法
            returnValue = point.proceed(args);
            // 后置通知
            afterRunningAdvice();
        } catch (Throwable throwable) {
    
    
            // 异常通知
            exceptionAdvice();
            throwable.printStackTrace();
        }finally {
    
    
            // 最终通知
            afterAdvice();
        }
        return returnValue;


        // 环绕通知
    }
}

2.3 > 接口及其实现类

public interface AccountDao {
    
    
    void saveAccount(Float money);
}
//--------------------------------------------------------------------------

public class AccountDaoImpl implements AccountDao {
    
    
    @Override
    public void saveAccount(Float money) {
    
    
        System.out.println("Save money is : " + money);
        int i= 4/0; // 触发异常
    }
}

3 > spring_ioc容器配置AOP切面 (bean.xml)

<?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
">
    <!-- 需要切入的类 -->
    <bean id="accountDao" class="com.xx01.dao.impl.AccountDaoImpl"/>

    <!-- 配置通知类 -->
    <bean id="logger" class="com.xx01.advice.Logger"/>
    
    <!-- 配置AOP切面 这里为AOP切面配置的开始 -->
    <aop:config>
        <!-- aop配置 
        	aspect_weaver:随便写 
        	ref :用与加强被通知(代理)对象 的类
        -->
        <aop:aspect id="aspect_weaver" ref="logger">
            <aop:pointcut id="pointcut_accountDaoImpl_saveMoney"
                          expression="
                          execution(public void com.xx01.dao.impl.AccountDaoImpl.saveAccount(Float))"/>
            <!-- 
            	acp:pointcut:切入点
            	id : 下方各种通知ref属性引入的pointcut-ref值
            	expression : 表达式
            	execution : 明确的表示出需要切入的方法名称在什么包,什么类内部
            -->

            <!-- 前置通知 -->
            <!-- 
            	method : 表示用与通知(加强)的方法
            	pointcut-ref : 被通知(加强)的方法
             -->
            <aop:before method="beforeAdvice" pointcut-ref="pointcut_accountDaoImpl_saveMoney"/>

            <!-- 后置通知 -->
            <aop:after-returning method="afterRunningAdvice" 
            pointcut-ref="pointcut_accountDaoImpl_saveMoney"/>

            <!-- 异常通知 -->
            <aop:after-throwing method="exceptionAdvice" 
            pointcut-ref="pointcut_accountDaoImpl_saveMoney"/>

            <!-- 异常通知 -->
            <aop:after method="afterAdvice" pointcut-ref="pointcut_accountDaoImpl_saveMoney"/>

        </aop:aspect>
    </aop:config>
</beans>

4 > 测试

4.1 > 测试结果(int i = 4 / 0;此异常存在)

public class Dome_01 {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext app = new ClassPathXmlApplicationContext("./bean.xml");
        AccountDao accountDao = app.getBean("accountDao", AccountDao.class);
        accountDao.saveAccount(1000f);
    }
}

在这里插入图片描述

4.2 > 测试结果(int i = 4 / 0;此异常不存在)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43309893/article/details/119713732