Spring AOP学习5种通知

一、Spring的AOP分为以下5种类型通知

①前置通知(Before):在连接点执行前执行该通知

②正常返回通知(AfterReturning):在连接点正常执行完后执行该通知,若目标方法执行异常则不会执行该通知

③异常返回通知(AfterThrowing):在连接点执行抛出异常时执行该通知

④后置通知(after/finally):在连接点执行完成后(不管成功、失败、异常)都会执行该通知

⑤环绕通知(Around):围绕在连接点前后

二、执行顺序

1、正常执行

①环绕通知:@Around

②前置通知:@Before

③连接点方法执行

④环绕通知:@Around

⑤后置通知:@After

⑥正常返回通知:@AfterReturning

  其他结论说出来也没意思,还是自己通过简单demo测试一下就出来了

三、测试

1.定义切面类

package com.atguigu.aop;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * @author: wangxiaobo
 * @create: 2020-11-05 15:20
 * 铺助类:切面类,做一些增强性的功能,为目标类里的目标方法进行功能增强
 **/
public class MyAspect01 {

    /**
     *
     */
    //前置通知方法
   public  void beforeMethod(){
       System.out.println ("前置通知方法:比如做安全性检查。。。开启事物。。。");
   }
    //后置通知方法
    public  void afterMethod(){
        System.out.println ("后置通知方法:比如提交事物。。。当产生异常,该方法不会被执行");
    }
    //异常通知方法
    public void logException() {
        System.out.println("异常通知方法:处理织入过程中,目标方法产生的异常问题,当目标产生异常,该方法会执行,该方法与后置方法是互斥的");
    }
    //最终通知方法
    public void after() {
        System.out.println("最终通知方法:无论目标方法是否产生异常,该方法都会执行");
    }
    //环绕通知方法
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("在环绕通知方法前面执行的部分。。。");
        Object proceed = joinPoint.proceed ();
        System.out.println("在环绕通知方法后面执行的部分。。。");
        return proceed;
    }

}

2.定义service接口并实现

service接口

public interface UserService {
    void save();
    void delete();
    void update();
    void  find();
}

实现类

package com.atguigu.service.impl;

import com.atguigu.service.UserService;

/**
 * @author: wangxiaobo
 * @create: 2020-11-05 14:39
 **/
public class UserServiceimpl implements UserService {
    @Override
    public void save() {
        System.out.println ("核心业务:添加功能。。。。");
//      int i = 1/ 0;

    }

    @Override
    public void delete() {
        System.out.println ("核心业务:删除功能。。。。");
    }

    @Override
    public void update() {
        System.out.println ("核心业务:修改功能。。。。");
    }

    @Override
    public void find() {
        System.out.println ("核心业务:查询功能。。。。");
    }
}

spring-aop.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"
       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">

     <!-- 注册目标类-->
    <bean name="userService" class="com.atguigu.service.impl.UserServiceimpl"/>
    <!-- 注册切面类 -->
    <bean name="aspect" class="com.atguigu.aop.MyAspect01"/>

    <!-- 将目标类和切面类合二为一 -->
     <aop:config>
          <aop:pointcut id="poincut" expression="execution(* com.atguigu.service.impl.*.*(..))"/>

         <!-- 绝对增强(通知)方法到织入到poincut表达式所决定的目标类的目标方法上。-->
         <aop:aspect ref="aspect">
             <!-- 把aspect切面类的after方法织入到poincut表达式所决定的目标类的目标方法的-->
             <aop:around method="around" pointcut-ref="poincut"/>

             <!-- 把aspect切面类的beforeMethod方法织入到poincut表达式所决定的目标类的目标方法的前面。-->
             <aop:before method="beforeMethod" pointcut-ref="poincut"/>

             <!-- 把aspect切面类的afterMethod方法织入到poincut表达式所决定的目标类的目标方法的后面-->
             <aop:after-returning method="afterMethod" pointcut-ref="poincut"/>
             <!-- 把aspect切面类的logException方法织入到poincut表达式所决定的目标类的目标方法的后面-->
             <aop:after-throwing method="logException" pointcut-ref="poincut"/>
             <!-- 把aspect切面类的after方法织入到poincut表达式所决定的目标类的目标方法的后面-->
             <aop:after method="after" pointcut-ref="poincut"/>


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

 

3、测试

@SuppressWarnings ("resources")
@RunWith (SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-aop.xml")
public class IOCTest {

    @Autowired
    private UserService userService;

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

没有学不会的技术,只有不学习的人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值