第七讲:7.2 spring AOP后置-环绕

一,后置
1,studentServiceAspect类添加doAfter方法,

public void doAfter(JoinPoint jp){
       System.out.println("类名:"+jp.getTarget().getClass().getName());
       System.out.println("方法名:"+jp.getSignature().getName());
       System.out.println("参数:"+jp.getArgs()[0]);
       System.out.println("开始添加后置通知:……");

2,beans.xml添加after标签
<?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 id="studentServiceAspect" class="com.cruise.advice.StudentServiceAspect"></bean>

<bean id="studentService" class="com.cruise.service.impl.StudentServiceImpl"></bean>

<aop:config>
    <aop:aspect id="suibiandingyiaop" ref="studentServiceAspect">
       <aop:pointcut expression="execution(* com.cruise.service.*.*(..))"id="bussnessService"/>
       <aop:before method="doBefor" pointcut-ref="bussnessService"/>
       <aop:after method="doAfter" pointcut-ref="bussnessService"/>
    </aop:aspect>
</aop:config>
</beans>
3,测试-运行(代码同上)
二,环绕
1,studentServiceAspect类添加doAround方法,获取返回值,
Object object = pjp.proceed();的作用就是调用了一下方法,返回值就是方法的返回值。
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
       System.out.println("添加学生前……");
       Object object = pjp.proceed();
       System.out.println("添加学生后……");
       return object;
    }
2,beans.xml添加around标签
<?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 id="studentServiceAspect" class="com.cruise.advice.StudentServiceAspect"></bean>

<bean id="studentService" class="com.cruise.service.impl.StudentServiceImpl"></bean>

<aop:config>
    <aop:aspect id="suibiandingyiaop" ref="studentServiceAspect">
       <aop:pointcut expression="execution(* com.cruise.service.*.*(..))"id="bussnessService"/>
       <aop:before method="doBefor" pointcut-ref="bussnessService"/>
       <aop:after method="doAfter" pointcut-ref="bussnessService"/>
       <aop:around method="doAround" pointcut-ref="bussnessService"/>
    </aop:aspect>
</aop:config>
</beans>
3,测试-运行(代码同上)
三,异常通知
1,studentServiceAspect类添加doAfterThrowing方法,

public void doAfterThrowing(JoinPoint pjp,Throwable ex) throws Throwable{
       System.out.println("异常通知:");
       System.out.println("异常信息:"+ex.getMessage());
    }
2,beans.xml添加throwing标签
<?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 id="studentServiceAspect" class="com.cruise.advice.StudentServiceAspect"></bean>

<bean id="studentService" class="com.cruise.service.impl.StudentServiceImpl"></bean>

<aop:config>
    <aop:aspect id="suibiandingyiaop" ref="studentServiceAspect">
       <aop:pointcut expression="execution(* com.cruise.service.*.*(..))"id="bussnessService"/>
       <aop:before method="doBefor" pointcut-ref="bussnessService"/>
       <aop:after method="doAfter" pointcut-ref="bussnessService"/>
       <aop:around method="doAround" pointcut-ref="bussnessService"/>
       <aop:after-throwing method="doAfterThrowing" pointcut-ref="bussnessService"throwing="ex"/>
    </aop:aspect>
</aop:config>
</beans>
3,StudentServiceImpl 添加测试数据System.out.println(1/0)
package com.cruise.service.impl;

import com.cruise.service.StudentService;

public class StudentServiceImpl implements StudentService{

    @Override
    public void addStudent(String name) {

       System.out.println("添加学生:"+name);
       System.out.println(1/0);
    }
}
4,测试-运行(同上)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值