SpringAOP中各种增强方法

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"
	xmlns:context="http://www.springframework.org/schema/context"
	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.zzu"></context:component-scan>
	<aop:aspectj-autoproxy proxy-target-class="false"></aop:aspectj-autoproxy>
</beans>
//待增强的目标类
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;
	}
}

//Test类
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zzu.calculator.ICalculatorService;

public class Test {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
		ICalculatorService calculator = applicationContext.getBean(ICalculatorService.class);
		int result = calculator.mul(1, 2);
		System.out.println(result);
		applicationContext.close();
	}
}

1、Before
前置增强(又称前置通知):在目标方法执行之前执行

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

@Component
@Aspect
public class CalculatorAspect {

	@Before("execution(public int com.zzu.calculator.CalculatorService.mul(int, int))")
	public void before(JoinPoint jp) {
		Object obj = jp.getTarget();
		Object[] args = jp.getArgs();
		System.out.println(obj.getClass().getName()+":The "+methodName+" method begins.");
		System.out.println(obj.getClass().getName()+":Parameters of the "+methodName+" method: ["+args[0]+","+args[1]+"]");
	}
}

执行Test类结果:

com.zzu.calculator.CalculatorService:The mul method begins.
com.zzu.calculator.CalculatorService:Parameters of the mul method: [1,2]
2

2、After
保留Before,查看整体效果。

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.springframework.stereotype.Component;

@Component
@Aspect
public class CalculatorAspect {

	//前置增强
	@Before("execution(public int com.zzu.calculator.CalculatorService.mul(int, int))")
	public void before(JoinPoint jp) {
		Object obj = jp.getTarget();
		Object[] args = jp.getArgs();
		String methodName = jp.getSignature().getName();
		System.out.println(obj.getClass().getName()+":The "+methodName+" method begins.");
		System.out.println(obj.getClass().getName()+":Parameters of the "+methodName+" method: ["+args[0]+","+args[1]+"]");
	}
	//后置增强
	@After("execution(public int com.zzu.calculator.CalculatorService.mul(int,int))")
	public void after(JoinPoint jp) {
		Object obj = jp.getTarget();
		String methodName = jp.getSignature().getName();
		System.out.println(obj.getClass().getName()+":The "+methodName+" method ends.");
	}
}

执行Test类结果:

com.zzu.calculator.CalculatorService:The mul method begins.
com.zzu.calculator.CalculatorService:Parameters of the mul method: [1,2]
com.zzu.calculator.CalculatorService:The mul method ends.
2

后置增强(又称后置通知):在目标方法执行后执行,无论目标方法运行期间是否出现异常。
例如我们在目标类的方法中添加异常:

if(true) {
	throw new ArithmeticException("两数之积不能为0");
}

在这里插入图片描述
出现异常,但是并没有影响后置增强的输出。
注意:后置增强无法获取目标方法执行结果,可以在返回增强中获取。
3、AfterReturning
返回增强(又称返回通知):在目标方法正常结束后执行,可以获取目标方法的执行结果。
保留前置增强和后置增强,查看整体效果

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

@Component
@Aspect
public class CalculatorAspect {

	//前置增强
	@Before("execution(public int com.zzu.calculator.CalculatorService.mul(int, int))")
	public void before(JoinPoint jp) {
		Object obj = jp.getTarget();
		Object[] args = jp.getArgs();
		String methodName = jp.getSignature().getName();
		System.out.println(obj.getClass().getName()+":The "+methodName+" method begins.");
		System.out.println(obj.getClass().getName()+":Parameters of the "+methodName+" method: ["+args[0]+","+args[1]+"]");
	}
	//返回增强
	@AfterReturning(value="execution(public int com.zzu.calculator.CalculatorService.mul(int,int))",returning="result")
	public void afterReturning(JoinPoint jp,Object result) {
		Object obj = jp.getTarget();
		String methodName = jp.getSignature().getName();
		System.out.println(obj.getClass().getName()+":Result of the "+methodName+" method:"+result);
	}
	//后置增强
	@After("execution(public int com.zzu.calculator.CalculatorService.mul(int,int))")
	public void after(JoinPoint jp) {
		Object obj = jp.getTarget();
		String methodName = jp.getSignature().getName();
		
		System.out.println(obj.getClass().getName()+":The "+methodName+" method ends.");
	}
}

执行Test类结果:

com.zzu.calculator.CalculatorService:The mul method begins.
com.zzu.calculator.CalculatorService:Parameters of the mul method: [1,2]
com.zzu.calculator.CalculatorService:The mul method ends.
com.zzu.calculator.CalculatorService:Result of the mul method:2
2

发现上面的最后两条信息与我们期望顺序相反,这与After和AfterReturning的执行顺序有关,一般来说更改不了。
4、AfterThrowing
异常增强(又称异常通知):目标方法抛出异常之后执行,可以访问到异常对象,且可以指定在出现哪种异常时才执行增强代码

package com.zzu.calculator;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class CalculatorAspect {

	//前置增强
	@Before("execution(public int com.zzu.calculator.CalculatorService.mul(int, int))")
	public void before(JoinPoint jp) {
		Object obj = jp.getTarget();
		Object[] args = jp.getArgs();
		String methodName = jp.getSignature().getName();
		System.out.println(obj.getClass().getName()+":The "+methodName+" method begins.");
		System.out.println(obj.getClass().getName()+":Parameters of the "+methodName+" method: ["+args[0]+","+args[1]+"]");
	}
	//返回增强
	@AfterReturning(value="execution(public int com.zzu.calculator.CalculatorService.mul(int,int))",returning="result")
	public void afterReturning(JoinPoint jp,Object result) {
		Object obj = jp.getTarget();
		String methodName = jp.getSignature().getName();
		System.out.println(obj.getClass().getName()+":Result of the "+methodName+" method:"+result);
	}
	//异常增强
	@AfterThrowing(value="execution(public int com.zzu.calculator.CalculatorService.mul(int,int))",throwing="exception")
	public void afterThrowing(JoinPoint jp,NullPointerException exception) {
		Object obj = jp.getTarget();
		String methodName = jp.getSignature().getName();
		System.out.println(obj.getClass().getName()+":Exception of the method "+methodName+": "+exception.getMessage());
	}
	//后置增强
	@After("execution(public int com.zzu.calculator.CalculatorService.mul(int,int))")
	public void after(JoinPoint jp) {
		Object obj = jp.getTarget();
		String methodName = jp.getSignature().getName();
		
		System.out.println(obj.getClass().getName()+":The "+methodName+" method ends.");
	}
}

在目标方法中添加代码:

if(true) {
	throw new ArithmeticException("两数之积不能为0");
}

执行Test类结果:

com.zzu.calculator.CalculatorService:The mul method begins.
com.zzu.calculator.CalculatorService:Parameters of the mul method: [1,2]
com.zzu.calculator.CalculatorService:The mul method ends.
Exception in thread "main" java.lang.ArithmeticException: 两数之积不能为0

发现没有结果输出,也就是说异常出现时目标方法中的result没有返回,同时返回增强没有执行。
到此我们得出执行顺序:前置增强–>后置增强–>返回增强或异常增强
5、Around
Around是环绕增强,此注解可以完成以上所有注解的整合:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class CalculatorAspect {

	@Around("execution(public int com.zzu.calculator.CalculatorService.mul(int,int))")
	public Object result(ProceedingJoinPoint jp) {//环绕增强实现以上所有增强,
		Object result = null;
		Object obj = jp.getTarget();
		Object[] args = jp.getArgs();
		String methodName = jp.getSignature().getName();
		try {
			try {
				//前置增强
				System.out.println(obj.getClass().getName()+":The mul method begins.");
				System.out.println(obj.getClass().getName()+":Parameters of the mul method: ["+args[0]+","+args[1]+"]");
				result = jp.proceed();//调用目标对象方法
			} finally {
				//后置增强
				System.out.println(obj.getClass().getName()+":The mul method ends.");
			}
			//返回增强
			System.out.println(obj.getClass().getName()+":Result of the "+methodName+" method:"+result);
		} catch (Throwable e) {
			//异常增强
			System.out.println(obj.getClass().getName()+":Exception of the method "+methodName+": "+e.getMessage());
		}
		return result;
	}
}

执行Test类结果:

com.zzu.calculator.CalculatorService:The mul method begins.
com.zzu.calculator.CalculatorService:Parameters of the mul method: [1,2]
Exception in thread "main" org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int com.zzu.calculator.ICalculatorService.mul(int,int)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:227)
	at com.sun.proxy.$Proxy8.mul(Unknown Source)
	at com.zzu.test.Test.main(Test.java:12)
com.zzu.calculator.CalculatorService:The mul method ends.
com.zzu.calculator.CalculatorService:Exception of the method mul: 两数之积不能为0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值