SpringAOP的5种增强类型应用讲解

SpringAOP的5种增强类型应用讲解

一.前言
spring框架中为我们提供的增强包括针对切面的增强和针对切入点的增强,对一个方法的增强底层使用的是动态代理,所以在学习springAop增强之前大家有必要先了解一下动态代理相关内容;
本文分别采用继承接口和使用配置文件来实现增强。

二.springAOP自带的增强类型
按照增强在目标类方法中的连接点位置,可以分为5种:
1.前置增强:org.springframework.aop.BeforeAdvice是前置增强顶层接口,因为Spring只持方法的增强,其子接口MethodBeforeAdvice是目前可用的前置增强。表示在目标方法执行前实施增强。

2.后置增强:org.springframework.aop.AfterReturningAdvice是目前可用的后置增强,表示在目标方法执行后实施增强。

3.环绕增强:org.aopalliance.intercept.MethodInterceptor代表了环绕增强,表示在目标方法执行前后实施增强。直接使用了AOP联盟定义的接口。

4.异常抛出增强:org.springframework.aop.ThrowsAdvice代表了异常抛出增强,表示在目标方法抛出异常后实施增强。

5.引介增强:org.springframework.aop.IntroductionInterceptor代表引介增强,表示在目标类中添加一些新的方法和属性。

三.五种增强的具体使用方法

一,前置增强

1,我们先不用配置文件,先使用原始的方法–继承底层接口的形式来实现增强。配置文件或者注解的底层也是使用这些接口来生成代理的。
首先,创建maven工程,利用pom文件自动导入所需的包。
然后,编写目标类和增强类。
1实现接口方式实现前置增强

package com.jimmy.mvn.a.BeforeAdvice;

/**
 * 目标类(被增强类),say方法将被增强,其中say方法执行前后都可以是切入点(point-cut)
 */
public class Target {
   
    public void say() {
   
        System.out.println("我需要被增强!");
    }
}


package com.jimmy.mvn.a.BeforeAdvice;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

/**
 * 增强类,用于在切入点处进行增强
 * 
 * 该类实现了MethodBeforeAdvice接口,并实现了唯一的before()方法。
 * 
 */
public class Advice implements MethodBeforeAdvice{
   

    /**
     * method是目标类的方法
     * args是目标类方法的入参
     * target是目标类实例
     * 
     * before()方法会在目标类方法调用前执行。
     */
    public void before(Method method, Object[] args, Object target) throws Throwable {
   
        System.out.println("我是前置增强,很高兴为你服务!");
    }   
}


//测试方法
package com.jimmy.mvn.SpringAdvice;

import org.springframework.aop.framework.ProxyFactory;

import com.jimmy.mvn.a.BeforeAdvice.Advice;
import com.jimmy.mvn.a.BeforeAdvice.Target;

public class BeforeAdviceTest {
   
    public static void main(String[] args) {
   

        Target target = new Target();  // 目标类对象
        Advice advice = new Advice();  // 增强类对象

        ProxyFactory pf = new ProxyFactory(); // Spring提供的代理工厂
        pf.setTarget(target);  // 设置代理目标
        pf.addAdvice(advice);  // 为代理目标添加增强

        Target target2 = (Target) pf.getProxy(); // 生成代理实例
        target2.say();  // 代理对象再调用say()方法就能产生前置增强
    }
}

2使用xml配置文件的方式实现前置增强
首先,还是先创建maven工程,自动导入需要的包。
然后,创建目标类和增强类。

package com.jimmy.mvn.a.BeforeAdviceXML;

/**
 * 目标类。同上,很普通的一个类,实际开发中往往是一个业务类 
 */
public class Target {
   
    public void say() {
   
        System.out.println("我需要被增强!");
    }
}

package com.jimmy.mvn.a.BeforeAdviceXML;

/**
 * 增强类,该类不再继承任何接口,就是个普通的POJO类
 * 
 * 从外观上看,根本看不出来这个类要干嘛 
 */
public class Advice {
      
    public void beforeAdvice() {
   
        System.out.println("我是前置增强,很高兴为你服务!");
    }
}

然后再针对切入点编写配置文件

<?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-4.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <bean id="target" class="com.jimmy.mvn.a.BeforeAdviceXML.Target"></bean>
    <bean id="advice" class="com.jimmy.mvn.a.BeforeAdviceXML.Advice"></</
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值