Spring AOP面向切面编程(二)—AOP实现方式2—AspectJ

1.使用AspectJ方式实现

1. 新建类,不用实现

          1.1 类中方法名任意

public class MyAdvice {

            public void mybefore(String name1,int age1){

                     System.out.println("前置"+name1 );

                 }

            public void mybefore1(String name1){

                     System.out.println("前置:"+name1);

                 }

             public void myaftering(){

                     System.out.println("后置2");

                 }

              public void myafter(){

                     System.out.println("后置1");

                 }

               public void mythrow(){

                      System.out.println("异常");

                 }

               public Object myarround(ProceedingJoinPoint p) throws Throwable{

                      System.out.println("执行环绕");

                      System.out.println("环绕-前置");

                      Object result = p.proceed();

                      System.out.println("环绕后置");

                      return result;

                     }

  }

      1.2 配置spring配置文件

                 1.2.1 <aop:after/> 后置通知,是否出现异常都执行

                 1.2.2 <aop:after-returing/> 后置通知,只有当切点正确执行时执行

                 1.2.3 <aop:after/> <aop:after-returing/> <aop:after-throwing/>执行顺序和配置顺序有关

                 1.2.4 execution() 括号不能扩上args

                 1.2.5 中间使用and 不能使用&& springand解析成&&

                 1.2.6 args(名称) 名称自定义的.顺序和demo1(参数,参数)对应

                 1.2.7 <aop:before/> arg-names=”名称” 名称来源于expression=”” args(),名称必须一样

                             1.2.7.1  args() 有几个参数,arg-names里面必须有几个参数

                             1.2.7.2  arg-names=”” 里面名称必须和通知方法参数名对应

 <aop:config>

      <aop:aspect ref="myadvice">

         <aop:pointcut expression="execution(* com.mzx.test.Demo.demo1(String,int)) and args(name1,age1)" id="mypoint"/>

         <aop:pointcut expression="execution(* com.mzx.test.Demo.demo1(String)) and args(name1)" id="mypoint1"/>


         <aop:before method="mybefore" pointcut-ref="mypoint" arg-names="name1,age1"/>

         <aop:before method="mybefore1" pointcut-ref="mypoint1" arg-names="name1"/>    <!-- 前置  -->


    <!-- <aop:after method="myafter" pointcut-ref="mypoint"/>     <!--  后置 -->

    <aop:after-returning method="myaftering" pointcut-ref="mypoint"/>    <!--  环绕 -->

    <aop:after-throwing method="mythrow" pointcut-ref="mypoint"/>     <!--  异常 -->

    <aop:around method="myarround"  pointcut-ref="mypoint"/>-->

       </aop:aspect>

 </aop:config>

2. 配置异常通知的步骤(AspectJ方式)

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

2. spring中有AspectJ方式提供了异常通知的办法.

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

3. 实现步骤:

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

public class MyThrowAdvice{

         public void myexception(Exception e1){

                  System.out.println("执行异常通知"+e1.getMessage());

            }

  }

3.2 spring配置文件中配置

          3.2.1 <aop:aspect>ref属性表示:方法在哪个类中.

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

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

          3.2.4 throwing: 异常对象名,必须和通知中方法参数名相同(可以不在通知中声明异常对象)

    <bean id="mythrow" class="com.mzx.advice.MyThrowAdvice"></bean>

    <aop:config>

             <aop:aspect ref="mythrow">

                    <aop:pointcut  expression="execution(*  com.mzx.test.Demo.demo1())"  id="mypoint"/>

                    <aop:after-throwing  method="myexception"  pointcut-ref="mypoint"  throwing="e1"/>

             </aop:aspect>

    </aop:config>

    <bean id="demo" class="com.mzx.test.Demo"></bean>

木子璇总结时刻:欢迎小伙伴们提出建议哦,如有错误,望大神指出哦,谢谢啦。

猜你喜欢

转载自blog.csdn.net/qq_41617744/article/details/80291355
今日推荐