Spring AOP面向切面编程(二)—AOP实现方式1—Schema-based

1.Schema-based实现步骤,前置通知,后置通知

1. 导入jar

     

2. 新建通知类

        2.1 新建前置通知

                  2.1.1 arg0: 切点方法对象Method对象

                  2.1.2 arg1: 切点方法参数

                  2.1.3 arg2:切点在哪个对象中

public class MyBeforeAdvice implements MethodBeforeAdvice {

       @Override

       public void before(Method arg0, Object[]  arg1, Object arg2) throws Throwable {

                  System.out.println("执行前置通知");

              }

    }

       2.2 新建后置通知

             2.2.1 arg0: 切点方法返回值

             2.2.2 arg1:切点方法对象

             2.2.3 arg2:切点方法参数

             2.2.4 arg3:切点方法所在类的对象

public class MyAfterAdvice implements AfterReturningAdvice {

        @Override

         public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {

                 System.out.println("执行后置通知");

                }

   }

3. 配置spring配置文件

  • 3.1 引入aop命名空间
  • 3.2 配置通知类的<bean>
  • 3.3 配置切面
  • 3.4 * 通配符,匹配任意方法名,任意类名,任意一级包名
  • 3.5 如果希望匹配任意方法参数  (..)

<?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="mybefore"  class="com.mzx.advice.MyBeforeAdvice"></bean>  

    <bean id="myafter"   class="com.mzx.advice.MyAfterAdvice"></bean>  


    <!-- 配置切面 -->

    <aop:config

             <!-- 配置切点 -->

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

             <!-- 通知 -->

             <aop:advisor  advice-ref="mybefore"  pointcut-ref="mypoint"/>

             <aop:advisor  advice-ref="myafter" pointcut-ref="mypoint"/>

    </aop:config>


    <!-- 配置Demo类,测试使用 -->

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

</beans>

4. 编写测试代码

public class Test {

          public static void main(String[] args) {

                 //Demo demo = new Demo();

                 //demo.demo1();

                 //demo.demo2();

                 //demo.demo3();

          ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

          Demo demo = ac.getBean("demo",Demo.class);

                 demo.demo1();

                 demo.demo2();

                 demo.demo3();

            }

}

5. 运行结果:

     

2.异常通知(Schema-based方式)

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

          1.1 必须自己写方法,且必须叫afterThrowing

          1.2 有两种参数方式

                    1.2.1 必须是1个或4个 

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

public class MyThrow  implements ThrowsAdvice{

           //public void afterThrowing(Method m, Object[] args, Object target, Exception ex) {

                  //System.out.println("执行异常通知");

                //}

        public void afterThrowing(Exception ex) throws Throwable {

                    System.out.println("执行异常通过-schema-base方式");

                         }

        }

2. ApplicationContext.xml配置

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

    <aop:config>

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

               <aop:advisor  advice-ref="mythrow"  pointcut-ref="mypoint" /> 

    </aop:config>

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

3.环绕通知(Schema-based方式)

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

         2. 实现步骤

                 2.1 新建一个类实现MethodInterceptor

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.2 配置applicationContext.xml

 <bean id="myarround" class="com.mzx.advice.MyArround"></bean>

    <aop:config>

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

            <aop:advisor advice-ref="myarround" pointcut-ref="mypoint" /> 

    </aop:config>

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

木子璇总结时刻:欢迎小伙伴们提出建议哦,如有错误,望大神指出哦,谢谢啦。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值