AOP的5种增强方式

AOP的5种增强方式

一、环境搭建

创建Java工程,建立如下文件;
在这里插入图片描述
各个java文件中源码如下:

CalculatorAspect类:

package com.jd.calculator;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Aspect			//	切入,Spring扫描到这个注解,表明这个方法就是一个切面  
@Service		//这个注解作用是将Calculator类创建对象并保存到Spring容器
public class CalculatorAspect {
	//前置增强  动态代理前边的代码
	@Before("execution(int mul(int,int))")//执行哪个方法的时候(之前)执行下面的方法,这里是乘法
	public void before(JoinPoint jp) {
		Object object = jp.getTarget();//得到目标对象
		Object [] args = jp.getArgs();//得到参数
		String name = jp.getSignature().getName();
		//前置内容
		System.out.println(object.getClass().getName()+":The mul method begins.");
		System.out.println(object.getClass().getName()+":Parameters of the "+name+" method: ["+args[0]+","+args[1]+"]");
	}
}

CalculatorService类:

package com.jd.calculator;

import org.springframework.stereotype.Service;

@Service
public class CalculatorService implements ICalculatorService {

	@Override
	public int mul(int a, int b) {
		int result = a*b;
		
		return result;
	}
}

ICalculatorService接口:

package com.jd.calculator;

public interface ICalculatorService {
	public int mul(int a, int b);
}

Test类:

package com.jd.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.calculator.ICalculatorService;

public class Test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		ICalculatorService calculatorService = applicationContext.getBean(ICalculatorService.class);
		int result = calculatorService.mul(1, 1);
		System.out.println("-->"+result);
	}
}

application.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:context="http://www.springframework.org/schema/context"
	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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<context:component-scan base-package="com.jd"></context:component-scan>
	<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>

5种增强方式

1. 前置增强(又称前置通知)

前置增强使用@Befor注解标识,增强方法优先于目标方法执行。

前置增强方法:

@Before("execution(int mul(int,int))")//执行方法之前执行下面的方法
	public void before(JoinPoint jp) {
		Object object = jp.getTarget();//得到目标对象
		Object [] args = jp.getArgs();//得到参数
		String name = jp.getSignature().getName();
		//前置增强内容
		System.out.println(object.getClass().getName()+":The "+name+" method begins.");
		System.out.println(object.getClass().getName()+":Parameters of the "+name+" method: ["+args[0]+","+args[1]+"]");
	}

运行Test类:控制台输出:
在这里插入图片描述

2. 后置增强(又称后置通知)

后置增强用@After标识,增强方法在目标方法执行后执行,无论目标方法运行期间是否出现异常。

后置增强方法:

@After("execution(int mul(int,int))")//执行哪个方法的时候(之前)执行下面的方法,这里是乘法
	public void after(JoinPoint jp) {
		Object object = jp.getTarget();//得到目标对象
		Object [] args = jp.getArgs();//得到参数
		String name = jp.getSignature().getName();
		//后置增强内容
		System.out.println(this.getClass().getName()+":The "+name+" method ends.");
	}

运行Test类:控制台输出:
在这里插入图片描述

3.返回增强(又称返回通知)

返回增强以@AfterReturning注解为标识,在目标方法正常结束后执行,可以获取目标方法的执行结果。

返回增强方法:

returning为目标方法执行完后的结果,与方法中Object a中参数的a相同

@AfterReturning(value="execution(int mul(..))",returning="a")
	public void afterReturning(JoinPoint jp,Object a) {
		Object object = jp.getTarget();
		String name= jp.getSignature().getName();
		//返回增强内容
		System.out.println(this.getClass().getName()+":Result of the "+name +" method:"+a);
	}

运行Test类:控制台输出:
在这里插入图片描述

4.异常增强(又称异常通知)

异常增强以@AfterThrowing注解为标识,目标方法抛出异常之后执行,可以访问到异常对象,且可以指定在出现哪种异常时才执行增强代码

先在目标类中定义一个异常:

package com.jd.calculator;

import org.springframework.stereotype.Service;

@Service
public class CalculatorService implements ICalculatorService {

	@Override
	public int mul(int a, int b) {
		int result = a*b;
		if(result==0) {
			throw new RuntimeException("异常!乘积不能为0!");
		}
	
		System.out.println("乘法运算结束,结果为"+result);
		return result;
	}
}

并修改Test类中mul方法的参数为(1,0)。
异常增强方法:

@AfterThrowing(value="execution(int mul(..))", throwing="e")
	public void afterThrowing(JoinPoint jp,Exception e) {
		Object object=jp.getTarget();
		String name= jp.getSignature().getName();
		System.out.println(this.getClass().getName()+":Result of the "+name +" method:"+e);
	}

运行Test类:控制台输出:

在这里插入图片描述

5.环绕增强(又称环绕通知)

环绕增强由@Around修饰,可以实现上述@Before,@After,@AfterReturning和@AfterThrowing四种增强效果,可以实现动态代理全过程。

注意:

  1. @Around修饰的方法中有ProceedingJoinPoint类型的参数,该变量可以决定是否执行目标方法;
  2. @Before、@After、@AfterRunning和@AfterThrowing修饰的方法没有返回值;而@Around修饰的方法必须有返回值,返回值为目标方法的返回值;
    环绕增强方法:

	@Around("execution(int mul(int,int))")
	public Object around(ProceedingJoinPoint jp) {
		Object result = null;
		Object obj = jp.getTarget();
		Object[] args = jp.getArgs();
		String name = jp.getSignature().getName().toString();
		try {
			try {
				//前置增强
				System.out.println(obj.getClass().getName()+":The "+name+" method begins.");
				System.out.println(obj.getClass().getName()+":Parameters of the "+name+" method: ["+args[0]+","+args[1]+"]");
				//执行目标类中的方法
				result = jp.proceed();
			} finally {
				//后置增强
				System.out.println(obj.getClass().getName()+":The "+name+" method ends.");
			}
			//返回增强
			System.out.println(obj.getClass().getName()+": The result of the "+name+" method is "+result);
		} catch (Throwable e) {
			//异常增强
			System.out.println(obj.getClass().getName()+":The "+name+" method has a exception:"+e.getMessage());
		}
		return result;
	}

运行Test类:设置Test类中mul的参数分别为不出现异常时的mul(1,1)和出现异常时的(1,0)控制台输出

不出现异常时:

在这里插入图片描述

出现异常时:
在这里插入图片描述

三、前置增强、后置增强、返回增强、异常增强的执行顺序

源码:

try {
	try {
		doBefore();// @Before注解所修饰的方法
		method.invoke();// 执行目标对象内的方法
	} finally {
		doAfter();// @After注解所修饰的方法
	}
	doAfterReturning();// @AfterReturning注解所修饰的方法
} catch (Exception e) {
	doAfterThrowing();// @AfterThrowing注解所修饰的方法
}

分析源码可知,先执行前置增强,然后执行目标方法,之后立即执行后置增强。因为后置增强在finally代码块里,所以无论是否出现异常,后置增强一定执行。如果出现异常,不再执行返回增强,下一步直接执行异常增强。如果不出现异常,则执行完后置增强后,执行返回增强,不执行执行异常增强

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值