欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring 学习(八):AOP异常通知和环绕通知

程序员文章站 2022-07-05 21:38:39
AOP异常通知和环绕通知一、配置异常通知的步骤(AspectJ方式)二、配置异常通知的步骤(Schema-based方式)三、配置环绕通知的步骤(Schema-based方式)一、配置异常通知的步骤(AspectJ方式)只有在切点报异常才能触发异常通知,否则无效在Spirng 中只有 AspectJ 方式提供可异常通知的方法实现步骤新建类,在类写任意名称的方法package com.zhang.advice;public class MyThrowAdvice {public voi...

一、配置异常通知的步骤(AspectJ方式)

  1. 只有在切点报异常才能触发异常通知,否则无效
  2. 在Spirng 中只有 AspectJ 方式提供可异常通知的方法
  3. 实现步骤
    1. 新建类,在类写任意名称的方法
      package com.zhang.advice;
      
      public class MyThrowAdvice {
      
      	public void myexception(Exception e) {
      		System.out.println("执行异常通知,异常信息:"+e.getMessage());
      	}
      }
      
    2. 在 spring 配置文件中配置
      2.1 < aop:aspect/>的 ref 属性表示:方法在哪个类中
      2.2 < aop:xxx/> 表示什么通知
      2.3 method: 当触发这个通知时,调用哪个方法
      2.4 在异常通知里,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"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.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"
              default-autowire="byName">
              
              <bean id="mythrow" class="com.zhang.advice.MyThrowAdvice"></bean>
              
              <aop:config>
          		<aop:aspect ref="mythrow">
          			<aop:pointcut expression="execution(* com.zhang.pojo.Demo.demo2(..))" id="mypoint"/>
          			<aop:after-throwing method="myexception" pointcut-ref="mypoint" throwing="e"/>
          		</aop:aspect>
          	</aop:config>
              
             <bean id="demo" class="com.zhang.pojo.Demo"></bean>
              
      </beans>
      

二、配置异常通知的步骤(Schema-based方式)

  1. 新建一个类实现 ThrowsAdvice接口
    1.1 必须自己写方法,且必须叫 afterThrowing
    1.2 有两种参数方式,必须是1个或者4个
    1.3 异常类型要与切点报的异常类型一致

    package com.zhang.advice;
    import java.lang.reflect.Method;
    
    import org.springframework.aop.ThrowsAdvice;
    
    public class MyThrowAdvice implements ThrowsAdvice{
    
    //	public void afterThrowing(Method arg0,Object[] arg1,Object arg2,Exception e) {
    //		System.out.println("执行异常通知");
    //	}
    	
    	public void afterThrowing(Exception e) {
    		System.out.println("执行异常通知");
    	}
    }
    
    
  2. 配置 spring 配置文件

    <?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"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.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"
            default-autowire="byName">
            
            <!-- 注入通知类 -->
            <bean id="myBefore" class="com.zhang.advice.MyBefore"></bean>
            <bean id="myAfter" class="com.zhang.advice.MyAfter"></bean>
            <bean id="mythrow" class="com.zhang.advice.MyThrowAdvice"></bean>
            
            <aop:config>
            	<aop:pointcut expression="execution(* com.zhang.pojo.Demo.demo2(..))" id="mypoint"/>
            	<aop:advisor advice-ref="mythrow" pointcut-ref="mypoint" />
        	</aop:config>
            
            
           <bean id="demo" class="com.zhang.pojo.Demo"></bean>
    	
    </beans>	
    

三、配置环绕通知的步骤(Schema-based方式)

把前置通知和后置通知都写到一个通知中,组成了环绕

  1. 新建一个类实现 MethodInterceptor接口

    package com.zhang.advice;
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    
    public class MyArround implements MethodInterceptor{
    
    	@Override
    	public Object invoke(MethodInvocation arg0) throws Throwable {
    		
    		System.out.println("环绕--前置");
    		Object result = arg0.proceed();//放行,执行切点程序
    		System.out.println("环绕--后置");
    		return result;
    	}
    
    }
    
  2. spring 配置文件

    <?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"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.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"
            default-autowire="byName">
            
            <!-- 注入通知类 -->
            <bean id="myBefore" class="com.zhang.advice.MyBefore"></bean>
            <bean id="myAfter" class="com.zhang.advice.MyAfter"></bean>
            <bean id="mythrow" class="com.zhang.advice.MyThrowAdvice"></bean>
            <bean id="myArround" class="com.zhang.advice.MyArround"></bean>
            
            <aop:config>
            	<aop:pointcut expression="execution(* com.zhang.pojo.Demo.demo2(..))" id="mypoint"/>
            	<aop:advisor advice-ref="myArround" pointcut-ref="mypoint" />
        	</aop:config>
            
            
           <bean id="demo" class="com.zhang.pojo.Demo"></bean>
            
    </beans>
    
    

本文地址:https://blog.csdn.net/qq_43478581/article/details/110825979