zoukankan      html  css  js  c++  java
  • AOP的异常通知

     

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

      1、只有当切点报异常才能触发异常通知

      2、在spring中有Aspectj 方式提供了异常通知方法

        2.1 如果希望通过 schema-base 实现需要按照特定的要求自己编写方法

      3、实现步骤:

        3.1 新建类,在类中写任意名称的方法    

    public class myExcetion {
        public void myexcetion(Exception e1){
            System.out.println("执行异常,tain"+e1.getMessage());
        }
    }

        3.2 在spring配置文件中配置

          3.2.1 <aop:aspect ref="">:ref= 表示在哪个类中

          3.2.2 <aop:xxxx> 什么标签表示什么通知

          3.2.3 method:表示当触发这个通知时,调用哪个方法

    <?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="myexcetion" class="com.bjsxt.myExcetion.myexcetion"></bean>
            <aop:config>
                <aop:aspect ref="myexcetion">
                    <aop:pointcut expression="execution(* com.bjsxt.test.Test.demo1())" id="mypoint"/>
                    <aop:after-throwing method="myExcetion" pointcut-ref="mypoint" throwing="e1"/>    throwing 表示异常处理的方法的参数名 (可以不再异常通知中声明异常对象)
                </aop:aspect>        
            </aop:config>
           <bean id="test" class="com.bjsxt.test.Test"></bean>    
      </beans>

     二、使用Schema-base 实现异常

      1、新建一个类实现ThrowsAdvice接口,且必须使用afterThrowing这个方法名。。

        1.1有两种参数方式

          必须有 1个 或 4个参数

        1.2 异常类型要与切点报的异常类型一致,

    public class Mythrow implements ThrowsAdvice{
       // public void afterThrowing(Exception ex) throws Throwable {
         //   System.out.println("执行异常通知11111");
        //}
    
        public void afterThrowing(Method m, Object[] args, Object target, Exception ex) {
            System.out.println("执行异常通知2222");
        }
    }

      2、在applicationContext.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 id="mythrow" class="com.bjsxt.advice.Mythrow"></bean>
            <bean id="demo" class="com.bjsxt.test.Demo"></bean>
            <aop:config>
                <aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" id="mypoint"/>
                <aop:advisor advice-ref="mythrow" pointcut-ref="mypoint"/>
            </aop:config>
      </beans>
    好好学习,天天向上。 努力工作,给自己的和家人一个满意的答案。。。。
  • 相关阅读:
    学习:ASP.NET中App_Code,App_Data等文件夹的作用(转)
    总结:CLR Via C#(第九章):属性与索引器
    总结:CLR Via C#(第八章):其他(方法传递、out、ref)
    Init Version
    Code 128 Barcode Font Advantage Package 的常见问题
    Skelta Workflow.NET 2004 R2
    GTP.NET 甘特图项目管理控件
    Code 128 Barcode Font Advantage Package 中如何手动加入起始符,结束符,校验符
    VectorDraw Professional v5.1.1.1043
    开篇
  • 原文地址:https://www.cnblogs.com/axu521/p/10141696.html
Copyright © 2011-2022 走看看