Spring AOP 前置通知 后置通知 异常通知 返回通知 以及环绕通知

  • 加入JAR包:                      

 

  • 在配置文件中加入AOP 的命名空间

 

  • 添加配置文件如下:

beans-aop-helloworld.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: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/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <!-- 配置自动扫描包 -->
    <context:component-scan base-package="spring.aop.helloworld"></context:component-scan>
    
    <!-- 让aspectj注解起作用: 自动为匹配的类生成代理对象 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

 

  • 把横切关注点的代码抽象到切面的类中
    • 切面首先是一个IOC中的bean, 即加入@Component注解
    • 切面还需要加入@Aspect注解
    • 为了保证打印的次序,我们还可以加入一个order注解 @order(2)
  • 在类中声明各种通知:
    • 申明一个方法, 用以指定在满足什么样的条件下加入注解。
    • 前置通知: 在方法前加入@Before注解
    • 后置通知: 在方法前加入@After注解,因为方法可能会出现异常,所以不能返回方法的返回值。
    • 返回通知: 在方法前加入@AfterReturning注解,可以得到方法的返回值
    • 异常通知: 在方法前加入@AfterThrowing注解,可以访问到方法出现的异常。
    • 环绕通知: 在方法前加入@Around,可以实现以上4种功能
  • 可以在通知方法中申明一个类型为JointPoint 的参数,然后就访问链接细节。如方法名称和参数值。
  • 具体代码如下:
    • ArithmeticCalculator.java
package spring.aop.helloworld;

public interface ArithmeticCalculator {
	public int add(int i, int j);

	public int sub(int i, int j);

	public int mul(int i, int j);

	public int div(int i, int j);
}

 

    • ArithmeticCalculatorImpl.java
package spring.aop.helloworld;

import org.springframework.stereotype.Component;

@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

	@Override
	public int add(int i, int j) {
		int result = i + j;
		return result;
	}

	@Override
	public int sub(int i, int j) {
		int result = i - j;
		return result;
	}

	@Override
	public int mul(int i, int j) {
		int result = i * j;
		return result;
	}

	@Override
	public int div(int i, int j) {
		int result = i/j;
		return result;
	}
}

 

    • LoggingAspect.java

 

package spring.aop.helloworld;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

//把这个类声明为一个切面:需要把该类放入到IOC容器中, 声明为一个切面
@Order(2)
@Aspect
@Component
public class LoggingAspect {
	/**
	 * 定义一个方法,用于声明切入点表达式,一般的,该方法中不需要再添加其他的代码。 使用@Pointcut来声明切入点表达式
	 * 后面的其他通知直接使用方法名来引用当前的切入点表达式
	 */
	@Pointcut("execution(public int spring.aop.helloworld.ArithmeticCalculator.*(int, int))")
	public void declareJoinPointExpression() {
	}

	// 声明该方法是一个前置通知:在目标方法开始之前执行
	@Before("declareJoinPointExpression()")
	public void beforeMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinPoint.getArgs());
		System.out.println("The " + methodName + " begins " + " args is: " + args);
	}

	// 后置通知:在目标方法发生之后执行(不论这个方法是否发生了异常)
	// 在后置通知中海不能访问目标方法执行的结果
	@After("declareJoinPointExpression()")
	public void afterMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinPoint.getArgs());
		System.out.println("The " + methodName + " ends " + methodName + " args is: " + args);
	}

	// 返回通知
	@AfterReturning(value = "declareJoinPointExpression()", returning = "result")
	public void afterReturning(JoinPoint joinPoint, Object result) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The " + methodName + " ends " + methodName + " return is: " + result);
	}

	// 在目标方法出现异常时,会执行的代码,可以访问到异常的对象,且可以指定在发生特定异常时才会打印
	@AfterThrowing(value = "declareJoinPointExpression()", throwing = "ex")
	public void afterThrowing(JoinPoint joinPoint, Exception ex) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The " + methodName + " occurs exception " + ex);
	}

	/**
	 * 环绕通知需要携带ProceedingJoinPoint类型的参数 环绕通知类似于动态代理的全过程: ProceedingJoinPoint
	 * 类型的参数可以决定是否执行目标方法 且环绕通知必须有返回值,返回值即为目标方法的返回值
	 * 
	 * @param pjd
	 */
	@Around("execution(public int spring.aop.helloworld.ArithmeticCalculator.*(..))")
	public Object aroundMethod(ProceedingJoinPoint pjd) {
		Object result = null;
		String methodName = pjd.getSignature().getName();
		try {
			// 前置通知
			System.out
					.println("@Around before ----> The " + methodName + " begin with " + Arrays.asList(pjd.getArgs()));
			result = pjd.proceed();
			// 返回通知
			System.out.println("@Around return ----> The " + methodName + " return with " + result);
		} catch (Throwable e) {
			System.out.println("@Around exception ----> The " + methodName + " occurs exception " + e);
			throw new RuntimeException(e);
		}
		// 后置通知
		System.out.println("@Around after ----> The " + methodName + " ends");
		return result;
	}
}

   

  • ValidateResult.javpackage spring.aop.helloworld;
import java.util.Arrays;
import java.util.List;

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

//Order注解用来指定切面打印的顺序 @Order(1) @Aspect @Component public class ValidateResult { @Before("spring.aop.helloworld.LoggingAspect.declareJoinPointExpression()") public void validateResult(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("Validate " + methodName + args);   
  • 注释:关于execute函数的匹配方法有多种写法,具体如下: 
//表示任意返回值
execution(public * spring.aop.helloworld.ArithmeticCalculator.*(int, int))

//表示包里面的所有类
execution(public * spring.aop.helloworld.*.*(int, int))

//匹配ArithmetricCalculator接口的所有公有方法
execution(public * ArithmeticCalculator.*(...))

//匹配ArithmetricCalculator接口中返回double类型数值的方法
execute public double ArithmetricClaculator.*(...)

//配第一个参数为double类型的方法, ...匹配任意数量任意类型的参数
execution public double ArithmetricCalculator.*(double, ...)

//匹配参数类型为double, double的方法
execution public double ArithmetricCalculator.*(double, double)

  

 

 

posted on 2016-01-30 11:50  我表情悠哉  阅读(1612)  评论(0编辑  收藏  举报

导航