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;
}
}
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>
补充:<aop:aspectj-autoproxy proxy-target-class="?">
值为True 或者 false的意义
Spring 处理AOP代理的问题,它有两套动态代理的实现,一个是JDK方法的实现,一个就是cglib,proxy-target-class就是决定使用哪个实现方式的开关,当该值是false时,使用JDK的实现方式,这也是默认的实现方式,反之则使用cglib的实现方式。当我把该值设置为true时,也就是使用cglib。使用JDK代理模式有一个限制,即它只能为接口创建代理实例
何时使用JDK还是CGLIB?
1)如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP。
2)如果目标对象实现了接口,可以强制使用CGLIB实现AOP。
3)如果目标对象没有实现了接口,必须采用CGLIB库,Spring会自动在JDK动态代理和CGLIB之间转换。
JDK和CGLIB动态代理总结
JDK代理是不需要第三方库支持,只需要JDK环境就可以进行代理,使用条件:
1)实现InvocationHandler
2)使用Proxy.newProxyInstance产生代理对象
3)被代理的对象必须要实现接口
CGLib必须依赖于CGLib的类库,但是它需要类来实现任何接口代理的是指定的类生成一个子类,
覆盖其中的方法,是一种继承但是针对接口编程的环境下推荐使用JDK的代理;
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四种增强效果,可以实现动态代理全过程。
注意:
- @Around修饰的方法中有ProceedingJoinPoint类型的参数,该变量可以决定是否执行目标方法;
- @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代码块里,所以无论是否出现异常,后置增强一定执行。如果出现异常,不再执行返回增强,下一步直接执行异常增强。如果不出现异常,则执行完后置增强后,执行返回增强,不执行执行异常增强