spring中AOP的四种常用通知

spring中AOP的四种常用通知,分别是

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

后置通知:在切入点方法执行后执行的方法

异常通知:在切入点方法动态代理中发生错误时执行的方法

最终通知:在切入点方法动态代理结束后执行的方法

 

代码演示

首先是创建一个UserService的接口和实现类

public interface UserService {
    public void save();
    public void update();
    public int delete(int i);
}
public class UserServiceImpl implements UserService {
    //模拟保存
    public void save() {
        System.out.println("保存方法运行了");
        int a = 1/0;
    }

    //模拟更新
    public void update() {
        System.out.println("更新方法运行了");
    }

    //模拟删除
    public int delete(int i) {
        System.out.println("删除方法运行了");
        return 0;
    }
}

定义一个日志类,包含4类的通知方法

public class Logger {
    public void BeforePrintLog(){
        System.out.println("前置通知");
    }
    public void AfterPrintLog(){
        System.out.println("后置通知");
    }
    public void ErrorPrintLog(){
        System.out.println("异常通知");
    }
    public void FinalPrintLog(){
        System.out.println("最终通知");
    }
}

然后在bean.xml进行相关的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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="UserServicr" class="com.itylm.service.impl.UserServiceImpl"></bean>
    <bean id="Logger" class="com.itylm.util.Logger"></bean>
    <aop:config>
        <aop:pointcut id="pt1" expression="execution( * com.itylm.service.impl.*.*(..))"/>
        <aop:aspect id="logAdvice" ref="Logger">
            <aop:before method="BeforePrintLog" pointcut-ref="pt1"></aop:before>
            <aop:after-returning method="AfterPrintLog" pointcut-ref="pt1"></aop:after-returning>
            <aop:after-throwing method="ErrorPrintLog" pointcut-ref="pt1"></aop:after-throwing>
            <aop:after method="FinalPrintLog" pointcut-ref="pt1"></aop:after>

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

这里先是配置了两个bean

<bean id="UserServicr" class="com.itylm.service.impl.UserServiceImpl"></bean>

<bean id="Logger" class="com.itylm.util.Logger"></bean>

然后开始配置AOP

首先是切入点表达式另外一种配置方式

<aop:pointcut id="pt1" expression="execution( * com.itylm.service.impl.*.*(..))"/>

开始配置通知

<aop:aspect id="logAdvice" ref="Logger">
      <!-- 配置前置通知:在切入点方法执行之前执行-->
     <aop:before method="BeforePrintLog" pointcut-ref="pt1"></aop:before>
      <!-- 配置后置通知:在切入点方法正常执行之后值。它和异常通知永远只能执行一个-->
     <aop:after-returning method="AfterPrintLog" pointcut-ref="pt1"></aop:after-returning>
      <!-- 配置异常通知:在切入点方法执行产生异常之后执行。它和后置通知永远只能执行一个-->
    <aop:after-throwing method="ErrorPrintLog" pointcut-ref="pt1"></aop:after-throwing>
      <!-- 配置最终通知:无论切入点方法是否正常执行它都会在其后面执行-->
    <aop:after method="FinalPrintLog" pointcut-ref="pt1"></aop:after>

</aop:aspect>

 

测试类:

public class test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        UserService userService = (UserService) ac.getBean("UserServicr");
        userService.save();
    }
}

运行结果:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值