从零开始学Spring(九)——AOP通知类型

      根据通知在目标方法执行前后位置的织入分为前置通知、后置通知、环绕通知、异常抛出通知以及最终通知

  • 前置通知

前置通知指的是在目标方法执行之前进行操作

  1. 编写切面类myAspect
public class myAspect {
    public void check(){
        System.out.println("权限校验");
    }
}
  1. 编写业务类Goods
public class Goods {
    public void save(){
        System.out.println("执行保存方法");
    }
}
  1. 配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config/>

    <bean id="goods" class="com.ph.demo2.Goods"/>
    <bean id="myaspect" class="com.ph.demo2.myAspect"/>

    <!--配置AOP完成对目标产生代理-->
    <aop:config>
        <!--配置切入点,也就是需要被增强的方法-->
        <aop:pointcut id="pointcut" expression="execution(* com.ph.demo2.Goods.save())"/>
       <!--配置切面,也就是增加的内容(通知)ref为哪一个类-->
        <aop:aspect ref="myaspect">
            <!--method为添加哪一个 方法 pointcut-ref表示添加到哪一个切入点-->
            <!--aop:before为通知类型,表示前置通知-->
            <aop:before method="check" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>
  1. 编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class testClass {
    @Autowired
    private Goods goods;
   @Test
    public void test(){
       goods.save();
   }
}

运行结果

 

  • 后置通知

在目标方法执行之后进行操作

  1. 编写切面类myAspect
public class myAspect {
    public void check(){
        System.out.println("权限校验");
    }
    public void log(){
        System.out.println("记录日志");
    }
}
  1. 编写业务类Goods
public class Goods {
    public void save(){
        System.out.println("执行保存方法");
    }
}
  1. 编写applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config/>

    <bean id="goods" class="com.ph.demo2.Goods"/>
    <bean id="myaspect" class="com.ph.demo2.myAspect"/>

    <!--配置AOP完成对目标产生代理-->
    <aop:config>
        <!--配置切入点,也就是需要被增强的方法-->
        <aop:pointcut id="pointcut" expression="execution(* com.ph.demo2.Goods.save())"/>
       <!--配置切面,也就是增加的内容(通知)ref为哪一个类-->
        <aop:aspect ref="myaspect">
            <!--method为添加哪一个 方法 pointcut-ref表示添加到哪一个切入点-->
            <!--aop:before为通知类型,表示前置通知-->
            <aop:before method="check" pointcut-ref="pointcut"/>
            <!--aop:after为通知类型,表示后置通知-->
            <aop:after method="log" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>
  1. 编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class testClass {
    @Autowired
    private Goods goods;
   @Test
    public void test(){
       goods.save();
   }
}

运行结果

  • 环绕通知

在目标方法执行之前和之后都要进行的操作,使用public Object around()

  1. 编写切面类myAspect
public class myAspect {
    public void check(){
        System.out.println("权限校验");
    }
    public void log(){
        System.out.println("记录日志");
    }
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前通知");
        Object proceed = joinPoint.proceed();
        System.out.println("环绕后通知");
        return proceed;
    }
}
  1. 编写业务类
public class Goods {
    public void save(){
        System.out.println("执行保存方法");
    }
}
  1. 编写applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config/>

    <bean id="goods" class="com.ph.demo2.Goods"/>
    <bean id="myaspect" class="com.ph.demo2.myAspect"/>

    <!--配置AOP完成对目标产生代理-->
    <aop:config>
        <!--配置切入点,也就是需要被增强的方法-->
        <aop:pointcut id="pointcut" expression="execution(* com.ph.demo2.Goods.save())"/>
       <!--配置切面,也就是增加的内容(通知)ref为哪一个类-->
        <aop:aspect ref="myaspect">
            <!--method为添加哪一个 方法 pointcut-ref表示添加到哪一个切入点-->
            <!--aop:before为通知类型,表示前置通知-->
            <aop:before method="check" pointcut-ref="pointcut"/>
            <!--aop:after-returning为通知类型,表示后置通知-->
            <aop:after-returning method="log" pointcut-ref="pointcut"/>
            <!--aop:around为通知类型,表示环绕通知-->
            <aop:around method="around" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>
  1. 编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class testClass {
    @Autowired
    private Goods goods;
   @Test
    public void test(){
       goods.save();
   }
}

运行结果

  • 异常抛出通知

在程序出现异常时进行操作,否则不操作

  1. 编写切面类myAspect
public class myAspect {
    public void check(){
        System.out.println("权限校验");
    }
    public void log(){
        System.out.println("记录日志");
    }
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前通知");
        Object proceed = joinPoint.proceed();
        System.out.println("环绕后通知");
        return proceed;
    }
    public void exceptionNotice(){
        System.out.println("程序出异常啦");
    }
}
  1. 编写业务类Goods
public class Goods {
    public void save(){
        int a=10/0;//写入异常
        System.out.println("执行保存方法");
    }
}
  1. 配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config/>

    <bean id="goods" class="com.ph.demo2.Goods"/>
    <bean id="myaspect" class="com.ph.demo2.myAspect"/>

    <!--配置AOP完成对目标产生代理-->
    <aop:config>
        <!--配置切入点,也就是需要被增强的方法-->
        <aop:pointcut id="pointcut" expression="execution(* com.ph.demo2.Goods.save())"/>
       <!--配置切面,也就是增加的内容(通知)ref为哪一个类-->
        <aop:aspect ref="myaspect">
            <!--method为添加哪一个 方法 pointcut-ref表示添加到哪一个切入点-->
            <!--aop:before为通知类型,表示前置通知-->
            <aop:before method="check" pointcut-ref="pointcut"/>
            <!--aop:after-returing 为通知类型,表示后置通知-->
            <aop:after-returning method="log" pointcut-ref="pointcut"/>
            <!--aop:around为通知类型,表示环绕通知-->
            <aop:around method="around" pointcut-ref="pointcut"/>
            <!--aop:after-throwing为通知类型,表示异常抛出通知-->
            <aop:after-throwing method="exceptionNotice" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>
  1. 编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class testClass {
    @Autowired
    private Goods goods;
   @Test
    public void test(){
       goods.save();
   }
}

运行结果

  • 最终通知

无论程序是否出现异常都会执行操作

  1. 编写切面类myAspect
public class myAspect {
    public void check(){
        System.out.println("权限校验");
    }
    public void log(){
        System.out.println("记录日志");
    }
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前通知");
        Object proceed = joinPoint.proceed();
        System.out.println("环绕后通知");
        return proceed;
    }
    public void exceptionNotice(){
        System.out.println("程序出异常啦");
    }
    public void finallyNotice(){
        System.out.println("程序执行完毕,释放资源");
    }
}
  1. 编写业务类Goods
public class Goods {
    public void save(){
        int a=10/0;
        System.out.println("执行保存方法");
    }
}
  1. 配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config/>

    <bean id="goods" class="com.ph.demo2.Goods"/>
    <bean id="myaspect" class="com.ph.demo2.myAspect"/>

    <!--配置AOP完成对目标产生代理-->
    <aop:config>
        <!--配置切入点,也就是需要被增强的方法-->
        <aop:pointcut id="pointcut" expression="execution(* com.ph.demo2.Goods.save())"/>
       <!--配置切面,也就是增加的内容(通知)ref为哪一个类-->
        <aop:aspect ref="myaspect">
            <!--method为添加哪一个 方法 pointcut-ref表示添加到哪一个切入点-->
            <!--aop:before为通知类型,表示前置通知-->
            <aop:before method="check" pointcut-ref="pointcut"/>
            <!--aop:after-returing 为通知类型,表示后置通知-->
            <aop:after-returning method="log" pointcut-ref="pointcut"/>
            <!--aop:around为通知类型,表示环绕通知-->
            <aop:around method="around" pointcut-ref="pointcut"/>
            <!--aop:after-throwing为通知类型,表示异常抛出通知-->
            <aop:after-throwing method="exceptionNotice" pointcut-ref="pointcut"/>
            <!--aop:after为通知类型,表示最终通知-->
            <aop:after method="finallyNotice" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>
  1. 编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class testClass {
    @Autowired
    private Goods goods;
   @Test
    public void test(){
       goods.save();
   }
}

程序出现异常运行结果

修改异常程序片段后运行结果

补充说明配置文件中的AOP切入点表达式的涵义

<!--配置切入点,也就是需要被增强的方法-->

  <aop:pointcut id="pointcut" expression="execution(* com.ph.demo2.Goods.save())"/>

它是基于execution函数完成格式为(【访问修饰符】 方法返回值 包名.类名.方法名(参数))

Public com.ph.demo2.Goods.save(..)  表示参数为任意参数

* com.ph.demo2.Goods.save(..)  *表示任意类型

* com.ph.demo2.Goods+.save(..)  +表示当前类的子类

* com.ph.demo2..*.*(..)  表示com.ph.demo2包以及子包下的所有类的所有方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值