当前位置:   article > 正文

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

aspect doafter

一,后置
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,测试-运行(同上)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/674995
推荐阅读
相关标签
  

闽ICP备14008679号