Spring-AOP五种注解的区别和联系

文章目录

  • @Before
  • @After
  • @AfterReturning
  • @AfterThrowing
  • @Around

导入AOP的jar包
配置xml文件


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

	<context:component-scan base-package="com.wl.calculater">context:component-scan>
	<aop:aspectj-autoproxy proxy-target-class="false">aop:aspectj-autoproxy>
beans>

目标类

@Service
public class CalculatorService implements ICalculatorService {
     
	@Override
	public int mul(int a, int b) {
     
		int result = a*b;
		return result;
	}
	@Override
	public int div(int a, int b) {
     
		int result = a/b;
		return result;
	}
}

@Before

  • 前置增强
@Aspect
@Component
public class CalculatorAspect {
     
	@Before("execution(* com.jd.calculater.CalculatorService.mul(..))")
	public void before(JoinPoint joinPoint) {
     
		System.out.println("前置增强");
		Object target = joinPoint.getTarget();
		Object[] args = joinPoint.getArgs();
		String name = joinPoint.getSignature().getName();
		System.out.println(target.getClass().getName() + ":Parameters of the " + name + " method: [" + args[0] + ","
				+ args[1] + "]");
	}

测试类

public class Test {
     
	public static void main(String[] args) {
     
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		ICalculatorService calculatorService = context.getBean(ICalculatorService.class);
		System.out.println(calculatorService.getClass());
		int result = calculatorService.mul(1, 1);
		System.out.println("结果为:"+result);
	}
}

结果

class com.sun.proxy.$Proxy8
前置增强
com.wl.calculater.CalculatorService:Parameters of the mul method: [1,1]
结果为:1

上面可以看出对mul方法进行了增强,并且是以JDK动态代理的方式

@After

  • 后置增强

切面类

@Aspect
@Component
public class CalculatorAspect {
     
	@Before("execution(public int mul(int,int))")
	public void before(JoinPoint joinPoint) {
     
		System.out.println("前置增强");
		Object target = joinPoint.getTarget();
		Object[] args = joinPoint.getArgs();
		String name = joinPoint.getSignature().getName();
		System.out.println(target.getClass().getName() + ":Parameters of the " + name + " method: [" + args[0] + ","
				+ args[1] + "]");
	}

	@After("execution(public int mul(int,int))")
	public void after(JoinPoint joinPoint) {
     
		System.out.println("后置增强");
		Object target = joinPoint.getTarget();
		String name = joinPoint.getSignature().getName();
		System.out.println(target.getClass().getName() + ":The " + name + " method ends.");
	}
}

测试类

public class Test {
     
	public static void main(String[] args) {
     
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		ICalculatorService calculatorService = context.getBean(ICalculatorService.class);
		System.out.println(calculatorService.getClass());
		int result = calculatorService.mul(1, 1);
		System.out.println("结果为:"+result);
	}
}

结果

class com.sun.proxy.$Proxy9
前置增强
com.wl.calculater.CalculatorService:Parameters of the mul method: [1,1]
后置增强
com.wl.calculater.CalculatorService:The mul method ends.
结果为:1

@AfterReturning

  • 返回增强,可以获得方法的返回值
@Aspect
@Component
public class CalculatorAspect {
     
	@Before("execution(public int mul(int,int))")
	public void before(JoinPoint joinPoint) {
     
		System.out.println("前置增强");
		Object target = joinPoint.getTarget();
		Object[] args = joinPoint.getArgs();
		String name = joinPoint.getSignature().getName();
		System.out.println(target.getClass().getName() + ":Parameters of the " + name + " method: [" + args[0] + ","
				+ args[1] + "]");
	}
	//这里需要注意注解中多了一个参数returning,下面参数列表中的参数名和返回值类型要对应起来
	@AfterReturning(value = "execution(public int mul(int,int))", returning = "result")
	public void afterReturning(JoinPoint joinPoint, Integer result) {
     
		System.out.println("返回增强");
		Object target = joinPoint.getTarget();
		String name = joinPoint.getSignature().getName();
		System.out.println(target.getClass().getName() + ":Result of the " + name + " method:" + result);
	}

	@After("execution(public int mul(int,int))")
	public void after(JoinPoint joinPoint) {
     
		System.out.println("后置增强");
		Object target = joinPoint.getTarget();
		String name = joinPoint.getSignature().getName();
		System.out.println(target.getClass().getName() + ":The " + name + " method ends.");
	}
}

测试类

public class Test {
     
	public static void main(String[] args) {
     
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		ICalculatorService calculatorService = context.getBean(ICalculatorService.class);
		System.out.println(calculatorService.getClass());
		int result = calculatorService.mul(1, 1);
		System.out.println("结果为:"+result);
	}
}

结果

class com.sun.proxy.$Proxy10
前置增强
com.wl.calculater.CalculatorService:Parameters of the mul method: [1,1]
后置增强
com.wl.calculater.CalculatorService:The mul method ends.
返回增强
com.wl.calculater.CalculatorService:Result of the mul method:1
结果为:1

@AfterThrowing

  • 异常增强
@Aspect
@Component
public class CalculatorAspect {
     
	@Before("execution(public int mul(int,int))")
	public void before(JoinPoint joinPoint) {
     
		System.out.println("前置增强");
		Object target = joinPoint.getTarget();
		Object[] args = joinPoint.getArgs();
		String name = joinPoint.getSignature().getName();
		System.out.println(target.getClass().getName() + ":Parameters of the " + name + " method: [" + args[0] + ","
				+ args[1] + "]");
	}

	@AfterReturning(value = "execution(public int mul(int,int))", returning = "result")
	public void afterReturning(JoinPoint joinPoint, Integer result) {
     
		System.out.println("返回增强");
		Object target = joinPoint.getTarget();
		String name = joinPoint.getSignature().getName();
		System.out.println(target.getClass().getName() + ":Result of the " + name + " method:" + result);
	}

	@After("execution(public int mul(int,int))")
	public void after(JoinPoint joinPoint) {
     
		System.out.println("后置增强");
		Object target = joinPoint.getTarget();
		String name = joinPoint.getSignature().getName();
		System.out.println(target.getClass().getName() + ":The " + name + " method ends.");
	}
	//throwing对应的值要和参数列表中的参数名对应
	@AfterThrowing(value = "execution(public int mul(int,int))", throwing = "exception")
	public void afterThrowing(JoinPoint joinPoint, RuntimeException exception) {
     
		System.out.println("异常增强");
		Object target = joinPoint.getTarget();
		String name = joinPoint.getSignature().getName();
		System.out.println("There is an RuntimeException in " + name + ":" + exception.getMessage());
	}

测试类

public class Test {
     
	public static void main(String[] args) {
     
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		ICalculatorService calculatorService = context.getBean(ICalculatorService.class);
		System.out.println(calculatorService.getClass());
		int result = calculatorService.mul(1, 1);
		System.out.println("结果为:"+result);
	}
}

结果

class com.sun.proxy.$Proxy11
前置增强
com.wl.calculater.CalculatorService:Parameters of the mul method: [1,1]
后置增强
com.wl.calculater.CalculatorService:The mul method ends.
返回增强
com.wl.calculater.CalculatorService:Result of the mul method:1
结果为:1

只有出现异常的情况话,才会出现异常增强,在CalculatorService中添加代码

@Service
public class CalculatorService implements ICalculatorService {
     
	@Override
	public int mul(int a, int b) {
     
		int result = a*b;
		//当a,b互为倒数的时候抛出异常
		if(result ==  1) {
     
			throw new RuntimeException("a和b互为倒数");
		}
		return result;
	}
	@Override
	public int div(int a, int b) {
     
		int result = a/b;
		return result;
	}
}

结果

class com.sun.proxy.$Proxy11
前置增强
com.wl.calculater.CalculatorService:Parameters of the mul method: [1,1]
后置增强
com.wl.calculater.CalculatorService:The mul method ends.
异常增强
There is an RuntimeException in mul:a和b互为倒数
Exception in thread "main" java.lang.RuntimeException: a和b互为倒数

@Around

  • 环绕增强,环绕增强可以实现@Before,@After,@AfterReturning和@AfterThrowing增强效果
@Aspect
@Component
public class CalculatorAspect {
     
//	@Before("execution(public int mul(int,int))")
//	public void before(JoinPoint joinPoint) {
     
//		System.out.println("前置增强");
//		Object target = joinPoint.getTarget();
//		Object[] args = joinPoint.getArgs();
//		String name = joinPoint.getSignature().getName();
//		System.out.println(target.getClass().getName() + ":Parameters of the " + name + " method: [" + args[0] + ","
//				+ args[1] + "]");
//	}
//
//	@AfterReturning(value = "execution(public int mul(int,int))", returning = "result")
//	public void afterReturning(JoinPoint joinPoint, Integer result) {
     
//		System.out.println("返回增强");
//		Object target = joinPoint.getTarget();
//		String name = joinPoint.getSignature().getName();
//		System.out.println(target.getClass().getName() + ":Result of the " + name + " method:" + result);
//	}
//
//	@After("execution(public int mul(int,int))")
//	public void after(JoinPoint joinPoint) {
     
//		System.out.println("后置增强");
//		Object target = joinPoint.getTarget();
//		String name = joinPoint.getSignature().getName();
//		System.out.println(target.getClass().getName() + ":The " + name + " method ends.");
//	}
//	@AfterThrowing(value = "execution(public int mul(int,int))", throwing = "exception")
//	public void afterThrowing(JoinPoint joinPoint, RuntimeException exception) {
     
//		System.out.println("异常增强");
//		Object target = joinPoint.getTarget();
//		String name = joinPoint.getSignature().getName();
//		System.out.println("There is an RuntimeException in " + name + ":" + exception.getMessage());
//	}
	@Around("execution(public int mul(int,int))")
	public Object around(ProceedingJoinPoint joinPoint) {
     
		Integer result = 0;
		Object target = joinPoint.getTarget();
		String name = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();
		try {
     
			try {
     
				System.out.println("前置增强");
				System.out.println(target.getClass().getName() + ":The " + name + " method begins.");
				System.out.println(target.getClass().getName() + ":Parameters of the " + name + " method: [" + args[0]
						+ "," + args[1] + "]");
				result = (Integer) joinPoint.proceed();
			} finally {
     
				System.out.println("后置增强");
				System.out.println(target.getClass().getName() + ":The " + name + " method ends.");
			}
			System.out.println("返回增强");
			System.out.println(target.getClass().getName() + ":Result of the " + name + " method:" + result);
			return result;
		} catch (Throwable e) {
     
			System.out.println("异常增强");
			System.out.println("There is an RuntimeException in " + name + ":" + e.getMessage());
			e.printStackTrace();
			return null;
		}
	}
}

测试类

public class Test {
     
	public static void main(String[] args) {
     
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		ICalculatorService calculatorService = context.getBean(ICalculatorService.class);
		System.out.println(calculatorService.getClass());
		int result = calculatorService.mul(1, 1);
		System.out.println("结果为:"+result);
	}
}

出现异常的结果

class com.sun.proxy.$Proxy8
前置增强
com.wl.calculater.CalculatorService:The mul method begins.
com.wl.calculater.CalculatorService:Parameters of the mul method: [1,1]
后置增强
com.wl.calculater.CalculatorService:The mul method ends.
异常增强
There is an RuntimeException in mul:a和b互为倒数
java.lang.RuntimeException: a和b互为倒数

不出现异常的结果

class com.sun.proxy.$Proxy8
前置增强
com.wl.calculater.CalculatorService:The mul method begins.
com.wl.calculater.CalculatorService:Parameters of the mul method: [1,2]
后置增强
com.wl.calculater.CalculatorService:The mul method ends.
返回增强
com.wl.calculater.CalculatorService:Result of the mul method:2
结果为:2
  • 注意当前置增强,后置增强,返回增强,异常增强同时出现的时候,如果出现异常,发挥增强不会执行,后置增强一定会执行(因为在finally代码块中)

你可能感兴趣的:(框架,AOP,Spring,Java,框架)