四种类型的通知(Advice)

下面来介绍 4 种类型的通知(Advice)。

Before Advice

method 运行前,将运行下面的代码。

HijackBeforeMethod.java 如下:

package com.shiyanlou.spring.aop.advice;

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class HijackBeforeMethod implements MethodBeforeAdvice {

    public void before(Method arg0, Object[] args, Object target)
            throws Throwable {
        System.out.println("HijackBeforeMethod : Before method hijacked!");

    }

}

在配置文件中加入新的 bean 配置 HijackBeforeMethod,然后创建一个新的代理(proxy),命名为 customerServiceProxy。target 定义你想劫持哪个 bean;interceptorNames 定义想用哪个 class(advice) 劫持 target。ApringAOPAdvice.xml 如下:

<beans xmlns = "http://www.springframework.org/schema/beans"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation = "http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id = "customerService" class = "com.shiyanlou.spring.aop.advice.CustomerService">
        <property name = "name" value = "Shiyanlou" />
        <property name = "url" value = "https://www.lanqiao.cn" />
    </bean>

    <bean id = "hijackBeforeMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackBeforeMethod" />

    <bean id = "customerServiceProxy" class = "org.springframework.aop.framework.ProxyFactoryBean">
        <property name = "target" ref = "customerService" />
        <property name = "interceptorNames">
            <list>
                <value>hijackBeforeMethodBean</value>
            </list>
        </property>
    </bean>

</beans>

用 Spring proxy 之前,必须添加 CGLIB 类库,在之前的 pom.xml 文件中,已经添加到了其中,以下是 pom.xml 依赖:

    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>3.2.9</version>
    </dependency>

App.java 如下:

package com.shiyanlou.spring.aop.advice;

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

public class App {

    public static void main(String[] args) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext(
                new String[] { "SpringAOPAdvice.xml" });

        CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");

        System.out.println("使用Spring AOP 如下");
        System.out.println("*************************");
        cust.printName();
        System.out.println("*************************");
        cust.printURL();
        System.out.println("*************************");

        try {
            cust.printThrowException();
        } catch (Exception e) {

        }

    }

}

输入命令:

mvn compile
mvn exec:java -Dexec.mainClass="com.shiyanlou.spring.aop.advice.App"

实验结果如下:

每一个 customerService 的 method 运行前,都将先执行 HijackBeforeMethod 的 before 方法。

After Returning Advice

创建一个实现了接口 AfterReturningAdvice 的 class,method 运行后,直到返回结果后,才运行下边的代码,如果没有返回结果,将不运行切入的代码。

HijackAfterMethod.java 如下:

package com.shiyanlou.spring.aop.advice;

import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;

public class HijackAfterMethod implements AfterReturningAdvice {

    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("HijackAfterMethod : After method hijacked!");
    }
}

修改 bean 配置文件,加入 hijackAfterMethodBean 配置,ApringAOPAdvice.xml 如下:

<beans xmlns = "http://www.springframework.org/schema/beans"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation = "http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id = "customerService" class = "com.shiyanlou.spring.aop.advice.CustomerService">
        <property name = "name" value = "lanqiao" />
        <property name = "url" value = "lanqiao.cn" />
    </bean>

    <bean id = "hijackBeforeMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackBeforeMethod" />
    <bean id = "hijackAfterMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackAfterMethod" />

    <bean id = "customerServiceProxy" class = "org.springframework.aop.framework.ProxyFactoryBean">
        <property name = "target" ref = "customerService" />
        <property name = "interceptorNames">
            <list>
                <value>hijackAfterMethodBean</value>
            </list>
        </property>
    </bean>

</beans>

现在再运行 App.java 后输出如下:

每一个 customerService 的 method 运行后,都将先执行 HijackAfterMethod 的 afterReturning 方法。

After Throwing Advice

创建一个实现了 ThrowsAdvice 接口的 class,劫持 IllegalArgumentException 异常,目标 method 运行时,抛出 IllegalArgumentException 异常后,运行切入的方法。HijackThrowExceptionMethod.java 如下:

package com.shiyanlou.spring.aop.advice;

import org.springframework.aop.ThrowsAdvice;

public class HijackThrowExceptionMethod implements ThrowsAdvice {

    public void afterThrowing(IllegalArgumentException e) throws Throwable {
        System.out.println("HijackThrowException : Throw exception hijacked!");
    }

}

修改 bean 配置文件,加入了 hijackThrowExceptionBean,ApringAOPAdvice.xml 如下:

<beans xmlns = "http://www.springframework.org/schema/beans"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation = "http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id = "customerService" class = "com.shiyanlou.spring.aop.advice.CustomerService">
        <property name = "name" value = "lanqiao" />
        <property name = "url" value = "lanqiao.cn" />
    </bean>

    <bean id = "hijackBeforeMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackBeforeMethod" />
    <bean id = "hijackAfterMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackAfterMethod" />
    <bean id = "hijackThrowExceptionBean" class = "com.shiyanlou.spring.aop.advice.HijackThrowExceptionMethod" />

    <bean id = "customerServiceProxy" class = "org.springframework.aop.framework.ProxyFactoryBean">
        <property name = "target" ref = "customerService" />
        <property name = "interceptorNames">
            <list>
                <value>hijackThrowExceptionBean</value>
            </list>
        </property>
    </bean>

</beans>

运行结果如下:

Around Advice

结合了以上 3 种形式的 Advice,创建一个实现了接口 MethodInterceptor 的 class,你必须通过 methodInvocation.proceed() 来调用原来的方法,即通过调用 methodInvocation.proceed() 来调用 CustomerService 中的每一个方法,当然也可以不调用原方法 HijackAroundMethod.java 如下:

package com.shiyanlou.spring.aop.advice;

import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class HijackAroundMethod implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("Method name : "
                + methodInvocation.getMethod().getName());
        System.out.println("Method arguments : "
                + Arrays.toString(methodInvocation.getArguments()));

        // 相当于 MethodBeforeAdvice
        System.out.println("HijackAroundMethod : Before method hijacked!");

        try {
            // 调用原方法,即调用 CustomerService 中的方法
            Object result = methodInvocation.proceed();

            // 相当于 AfterReturningAdvice
            System.out.println("HijackAroundMethod : After method hijacked!");

            return result;

        } catch (IllegalArgumentException e) {
            // 相当于 ThrowsAdvice
            System.out.println("HijackAroundMethod : Throw exception hijacked!");
            throw e;
        }
    }

}

修改 bean 配置文件,加入了 hijackAroundMethodBean,ApringAOPAdvice.xml 如下:

<beans xmlns = "http://www.springframework.org/schema/beans"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation = "http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id = "customerService" class = "com.shiyanlou.spring.aop.advice.CustomerService">
        <property name = "name" value = "lanqiao" />
        <property name = "url" value = "lanqiao.cn" />
    </bean>

    <bean id = "hijackBeforeMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackBeforeMethod" />
    <bean id = "hijackAfterMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackAfterMethod" />
    <bean id = "hijackThrowExceptionBean" class = "com.shiyanlou.spring.aop.advice.HijackThrowExceptionMethod" />
    <bean id = "hijackAroundMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackAroundMethod" />

    <bean id = "customerServiceProxy" class = "org.springframework.aop.framework.ProxyFactoryBean">
        <property name = "target" ref = "customerService" />
        <property name = "interceptorNames">
            <list>
                <value>hijackAroundMethodBean</value>
            </list>
        </property>
    </bean>
</beans>

运行结果:

posted @ 2020-11-13 21:12  上杉家主-上杉绘梨衣  阅读(404)  评论(0编辑  收藏  举报