四种常用通知类型

四种常用通知类型

在这里插入图片描述

前置通知:在切入点方法之前执行

后置通知:在切入点方法正常执行之后执行【后置通知和异常通知只能执行一个】

异常通知:在切入点方法产生异常后执行

最终通知:无论切入点方法是否正常执行,它都会执行

日志类

package cn.luis.util;

/**
 * @ClassName Logger
 * @Description 用于记录日志的工具类,它里面提供了公共的代码
 * @Author L
 * @Date 2020.04.05 1:18
 * @Version 1.0
 * @Remark TODO
 **/
public class Logger {

    /**
     * 前置通知
     */
    public void beforePrintLog() {
        System.out.println("前置通知开始记录日志...");
    }

    /**
     * 后置通知
     */
    public void aftereReturnPrintLog() {
        System.out.println("后置通知开始记录日志...");
    }

    /**
     * 异常通知
     */
    public void afterThrowingPrintLog() {
        System.out.println("异常通知开始记录日志...");
    }

    /**
     * 最终通知
     */
    public void afterPrintLog() {
        System.out.println("最终通知开始记录日志...");
    }
}

账户的业务层实现类

package cn.luis.service.impl;

import cn.luis.service.IAccountService;

/**
 * @ClassName AccountServiceImpl
 * @Description 账户的业务层实现类
 * @Author L
 * @Date 2020.04.05 1:16
 * @Version 1.0
 * @Remark TODO
 **/
public class AccountServiceImpl implements IAccountService{

    @Override
    public void saveAccount() {
        System.out.println("执行了保存");
        int i = 1/0;
    }

    @Override
    public void updateAccount(int i) {
        System.out.println("执行了更新"+i);
    }

    @Override
    public int deleteAccount() {
        System.out.println("执行了删除");
        return 0;
    }
}

配置文件

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--Spring的IOC配置:把service对象配置起来-->
    <!--1. 有一个需要增强方法的service,需要加个日志-->
    <bean id="accountService" class="cn.luis.service.impl.AccountServiceImpl"></bean>
    <!--配置Logger类-->
    <!--2. 写了一个日志的通知,它里面有加日志的方法-->
    <bean id="logger" class="cn.luis.util.Logger"></bean>

    <!--配置AOP-->
    <aop:config>
        <!--配置切面-->
        <!--3. 配置的这个切面引用了我们配置的通知2-->
        <aop:aspect id="logAdvice" ref="logger">
            <!--配置前置通知:在切入点方法之前执行-->
            <aop:before method="beforePrintLog" pointcut="execution( public void cn.luis.service.impl.AccountServiceImpl.saveAccount())"></aop:before>
            <!--配置后置通知:在切入点方法正常执行之后执行【后置通知和异常通知只能执行一个】-->
            <aop:after-returning method="aftereReturnPrintLog" pointcut="execution( public void cn.luis.service.impl.AccountServiceImpl.saveAccount())"></aop:after-returning>
            <!--配置异常通知:在切入点方法产生异常后执行-->
            <aop:after-throwing method="afterThrowingPrintLog" pointcut="execution( public void cn.luis.service.impl.AccountServiceImpl.saveAccount())"></aop:after-throwing>
            <!--配置最终通知:无论切入点方法是否正常执行,它都会执行-->
            <aop:after method="afterPrintLog" pointcut="execution( public void cn.luis.service.impl.AccountServiceImpl.saveAccount())"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

测试AOP配置

package cn.luis.test;

import cn.luis.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @ClassName AOPTest
 * @Description 测试AOP配置
 * @Author L
 * @Date 2020.04.05 1:50
 * @Version 1.0
 * @Remark TODO
 **/
public class AOPTest {

    public static void main(String[] args) {
        // 1.获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        // 2.获取对象
        IAccountService as = (IAccountService) ac.getBean("accountService");
        // 3.执行方法
        as.saveAccount();
    }
}

结果:

前置通知开始记录日志...
执行了保存
异常通知开始记录日志...
最终通知开始记录日志...
Exception in thread "main" java.lang.ArithmeticException: / by zero
    ...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值