Spring第五章:AOP(Schema-based)

AOP

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

java

  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      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"
        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 實體類express

 

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)
相關文章
相關標籤/搜索