Spring第五章:AOP(Schema-based)

AOP

链接:https://pan.baidu.com/s/1qS-AaZHSLUwxYSjc9PHDYA
提取码:np2q

  1.AOP:中文名称面向切面编程

  2.英文名称:(Aspect Oriented Programming)

  3.正常程序执行流程都是纵向执行流程

    3.1 又叫面向切面编程,在原有纵向执行流程中添加横切面

    3.2 不需要修改原有程序代码

      3.2.1 高扩展性

      3.2.2 原有功能相当于释放了部分逻辑.让职责更加明确.

      

 

  4.面向切面编程是什么?

    4.1 在程序原有纵向执行流程中,针对某一个或某一些方法添加通,形成横切面过程就叫做面向切面编程.

  5.常用概念

    5.1 原有功能: 切点, pointcut

    5.2 前置通知: 在切点之前执行的功能. before advice

    5.3 后置通知: 在切点之后执行的功能,after advice

    5.4 如果切点执行过程中出现异常,会触发异常通知.throws advice

    5.5 所有功能总称叫做切面.

    5.6 织入: 把切面嵌入到原有功能的过程叫做织入

  6.spring 提供了 3AOP 实现方式 

    6.1 Schema-based

      6.1.1 每个通知都需要实现接口或类

      6.1.2 配置 spring 配置文件时在<aop:config>配置

        6.1.2.1  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="mybefore" class="com.suncl.test.MyBeforeAdvice"></bean>
    <bean id="myafter" class="com.suncl.test.MyAfterAdvice"></bean>
    <bean id="myarround"  class="com.suncl.test.MyArround"></bean>
    <bean id="mythrow" class="com.suncl.test.MyThrow"></bean>

    <!-- 配置切面 -->
    <aop:config>
        <!-- 配置切点 -->
        <aop:pointcut expression="execution(* com.suncl.test.Demo.demo())" id="mypoint"/>

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

        <!-- 前置通知 -->
        <aop:advisor advice-ref="mybefore"  pointcut-ref="mypoint"/>

        <!-- 后置通知 -->
        <aop:advisor advice-ref="myafter"  pointcut-ref="mypoint"/>

        <!-- 环绕通知 -->
        <aop:advisor advice-ref="myarround" pointcut-ref="mypoint" />

        <!--异常通知-->
        <aop:advisor advice-ref="mythrow" pointcut-ref="mypoint1" />

    </aop:config>

    <!-- 配置 Demo 类,测试使用 -->
    <bean id="demo" class="com.suncl.test.Demo"></bean>

    <!--&lt;!&ndash; aspect方式配置异常通知 &ndash;&gt;-->
    <!--<bean id="myThrowAdvice" class="com.suncl.test.MyThrowAdvice"></bean>-->


</beans>

        6.1.2.2 实体类

 

package com.suncl.test;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by SCL-PC on 2019/2/28.
 */
public class MyBeforeAdvice implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("执行前置通知");
    }

}
package com.suncl.test;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

/**
 * Created by SCL-PC on 2019/2/28.
 */
public class MyAfterAdvice implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行后置通知");
    }
}
package com.suncl.test;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * Created by SCL-PC on 2019/2/28.
 */
public class MyArround implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("环绕-前置");
        Object result = methodInvocation.proceed();//放行,调用切点方式
        System.out.println("环绕-后置");
        return result;
    }
}
package com.suncl.test;

import org.springframework.aop.ThrowsAdvice;

/**
 * Created by SCL-PC on 2019/2/28.
 */
public class MyThrow implements ThrowsAdvice {
    public void afterThrowing(Exception ex) throws Throwable {
        System.out.println("MyThrow执行异常通过-schema-base 方式 ");
    }
}
package com.suncl.test;

/**
 * Created by SCL-PC on 2019/2/28.
 */
public class Demo {

    public void demo(){
        System.out.println("执行了demo方法");
    }

    public void demo1(){
        System.out.println("执行了demo1方法");
        int i = 1/0;
    }

    public void demo2(){
        System.out.println("执行了demo2方法");
        int i = 1/0;
    }

}

        6.1.2.3 测试类 

package com.suncl.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by SCL-PC on 2019/2/28.
 */
public class Test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
         Demo demo = ac.getBean("demo",Demo.class);
        demo.demo();
        demo.demo1();
    }
}

        6.1.2.4 运行结果

信息: Loading XML bean definitions from class path resource [applicationContext.xml]
执行前置通知
环绕-前置
执行了demo方法
环绕-后置
执行后置通知
执行了demo1方法
MyThrow执行异常通过-schema-base 方式 
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.suncl.test.Demo.demo1(Demo.java:14)
posted @ 2019-03-07 22:44  reload  阅读(316)  评论(0编辑  收藏  举报