项目结构和相关代码解释参见Spring中AOP的实现
Test,application.xml中代码在下边所有增强中公用
package com.jd.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jd.calculator.ICalculatorService;
public class Test {
public static void main(String[] args) {
//application.xml中配置后。
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");//获得IOP容器,创建对象,立即执行application.xml中的代码,所以此行代码执行完毕已经得到动态代理对象。
ICalculatorService calculatorService=applicationContext.getBean(ICalculatorService.class); //jdk代理,此处用接口类。CGLIB代理用CalculatorService.class也可以。
System.out.println(calculatorService.getClass().getName());
int result=calculatorService.mul(1, 7);
System.out.println("---->"+result);
}
}
<?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="false"></aop:aspectj-autoproxy>
</beans>
前置增强
1,CalculatorAspect类中代码
package com.jd.calculator;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class CalculatorAspect {
@Before("execution(int mul(int, int))") //前置增强:在执行方法前执行该方法代码。execution(int mul(..))
public void before(JoinPoint jp) {
Object object=jp.getTarget(); //得到代理类
Object [] args=jp.getArgs(); //获取参数
String name=jp.getSignature().getName(); //方法名
System.out.println(this.getClass().getName()+":The "+name+" method begins.");
System.out.println(this.getClass().getName()+":Parameters of the "+name+" method: ["+args[0]+","+args[1]+"]");
}
}
2,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;
System.out.println(this.getClass().getName()+":Result of the mul method:"+result); //先得不到result,所以改行代码先不写。
System.out.println(this.getClass().getName()+":The mul method ends.");
return result;
}
@Override
public int div(int a, int b) {
System.out.println(this.getClass().getName()+":The div method begins.");
System.out.println(this.getClass().getName()+":Parameters of the div method: ["+a+","+b+"]");
int result = a/b;
System.out.println(this.getClass().getName()+":Result of the div method:"+result);
System.out.println(this.getClass().getName()+":The div method ends.");
return result;
}
}
执行Test类中的代码:在执行之前开始执行前置增强方法,输出如下结果
后置增强
在前置增强的基础上,添加后置增强
1,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.springframework.stereotype.Component;
@Aspect //切面:由切点(方法)组成。
@Component
public class CalculatorAspect {
@Before("execution(int mul(int, int))") //前置增强:在执行方法前执行该方法代码。execution(int mul(..))。(又称后置通知):在目标方法执行后执行,无论目标方法运行期间是否出现异常。注意:后置增强无法获取目标方法执行结果,可以在返回增强中获取
public void before(JoinPoint jp) {
Object object=jp.getTarget(); //得到代理类
Object [] args=jp.getArgs(); //获取参数
String name=jp.getSignature().getName(); //方法名
System.out.println(this.getClass().getName()+":The "+name+" method begins.");
System.out.println(this.getClass().getName()+":Parameters of the "+name+" method: ["+args[0]+","+args[1]+"]");
}
@After("execution(int mul(int, int))") //后置增强:在执行完方法后执行该方法代码。
public void after(JoinPoint jp) {
Object object=jp.getTarget();
String name=jp.getSignature().getName();
System.out.println(this.getClass().getName()+":The "+name+" method ends.");
}
}
2,CalculatorService 类中代码为去掉如下代码
System.out.println(this.getClass().getName()+":The mul method ends.");
依旧输出如下结果
结果增强
1,在前置增强和后置增强的基础上,在CalculatorAspect类中添加如下代码
@AfterReturning(value="execution(int mul(..))",returning="a") //1,returning="a"中的a指定将参数传给谁,与Object a中的a对应。2,execution(int *(..)):匹配所有的方法。3,(又称返回通知):在目标方法正常结束后执行,可以获取目标方法的执行结果。
public void afterReturn(JoinPoint joinPoint,Object a) {
Object object=joinPoint.getTarget();
String name=joinPoint.getSignature().getName();
System.out.println(this.getClass().getName()+":Result of the "+name+" method:"+a);
}
2,接着继续在CalculatorService 类中去掉如下代码
System.out.println(this.getClass().getName()+":Result of the mul method:"+result);
输出如下结果
上述问题是什么原因呢?解释见下边的四个注解执行顺序
异常增强
场景:如果当程序执行过程中出错,抛出异常。
1,在前边的基础上,CalculatorAspect类中继续增加如下代码
//出异常,after执行,afterReturning不执行?
@AfterThrowing(value="execution(int mul(..))",throwing="exception")
public void afterThrowing(JoinPoint joinPoint,Exception exception) { //此处的Exception要比抛出的异常类大。不然不可以触发该异常;前边为运行时异常(ArithmeticException),此处NullPointerException会出错。2,(又称异常通知):目标方法抛出异常之后执行,可以访问到异常对象,且可以指定在出现哪种异常时才执行增强代码
Object object=joinPoint.getTarget();
String name=joinPoint.getSignature().getName();
System.out.println(this.getClass().getName()+":The "+name+exception.getMessage());
}
2,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;
if(result==0) {
throw new RuntimeException("乘积不能为0"); //运行时异常。
}
return result;
}
@Override
public int div(int a, int b) {
System.out.println(this.getClass().getName()+":The div method begins.");
System.out.println(this.getClass().getName()+":Parameters of the div method: ["+a+","+b+"]");
int result = a/b;
System.out.println(this.getClass().getName()+":Result of the div method:"+result);
System.out.println(this.getClass().getName()+":The div method ends.");
return result;
}
}
Test中传参数,0与7,使得乘积为0 。执行代码得到如下结果,
为什么结果增强方法没有执行?解释见下边的四个注解执行顺序
四个注解执行顺序
@Before,@After,@AfterReturning,@AfterThrowing执行顺序,执行过程:
try {
try {
doBefore(); // @Before注解所修饰的方法,执行before方法。
method.invoke(); // 执行目标对象内的方法
} finally {
doAfter();// @After注解所修饰的方法 //此行代码无论前边代码是否出现异常,都会执行。
}
doAfterReturning();// @AfterReturning注解所修饰的方法 出现异常时,不会执行该行代码。因为无论异常出现与否,此行代码总是在doAfter();之后执行。
} catch (Exception e) {
doAfterThrowing();// @AfterThrowing注解所修饰的方法 出现异常,该代码执行。
}
1,因为finally中的代码总是会执行,且在AOP机制中,doAfter();方法执行在doAfterReturning();方法之前,所以在结果增强中输出时代码顺序会颠倒。
2,当程序还没有执行doAfterReturning();方法,出现异常时,该方法不会执行,直接捕获异常,执行异常增强方法,所以在异常增强中没有执行结果增强代码。
环绕增强
AspectJ一共支持5种类型的增强注解,除了@Before,@After,@AfterReturning和@AfterThrowing以外,还有一种增强注解——@Around,在@Around修饰的方法中可以实现@Before,@After,@AfterReturning和@AfterThrowing增强效果,可以实现动态代理全过程。
1,CalculatorAspect 中代码如下:
package com.jd.calculator;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class CalculatorAspect {
//环绕增强,用一个方法实现四种增强。
@Around("execution(int *(..))")
public Object around(ProceedingJoinPoint joinPoint) { //ProceedingJoinPoint是JoinPoint的子类,继承关系。
Object result=null;
Object object=joinPoint.getTarget();
Object [] args=joinPoint.getArgs();
String name=joinPoint.getSignature().getName();
try {
try {
//前置增强
System.out.println(this.getClass().getName()+":The "+name+" method begins.");
System.out.println(this.getClass().getName()+":Parameters of the "+name+" method: ["+args[0]+","+args[1]+"]");
result=joinPoint.proceed(); //该行代码执行,开始执行目标类中的方法。
} finally{
//后置增强:不论什么时候都执行。在spring的AOP机制中:后置增强在结果增强的前边。
System.out.println(this.getClass().getName()+":The "+name+" method ends.");
}
//结果增强:前边代码出现异常,该行代码不会执行。
System.out.println(this.getClass().getName()+":Result of the "+name+" method:"+result);
} catch (Throwable e) {
//异常增强:前边代码出现异常,捕获异常执行改行代码。
System.out.println(this.getClass().getName()+":The "+name+e.getMessage());
}
return result;
}
}
2,其他类中代码为异常增强时的代码,保持不变
3,结果如下
4,分析:
1)、@Before、@After、@AfterRunning和@AfterThrowing修饰的方法可以通过声明JoinPoint 类型参数变量获取目标方法的信息(方法名、参数列表等信息);@Around修饰的方法必须声明ProceedingJoinPoint类型的参数,该变量可以决定是否执行目标方法;
2)、@Before、@After、@AfterRunning和@AfterThrowing修饰的方法没有返回值;而@Around修饰的方法必须有返回值,返回值为目标方法的返回值;
引申
用添加事务的方式实现环绕增强
1,在CalculatorAspect 类中用如下代码代替前边的注解
@Around("@annotation(org.springframework.transaction.annotation.Transactional)") 加此注解,给方法添加@Transactional,就可以在调用添加了注解的方法时开启环绕增强。
2,给mul方法添加如下注解:不加如下注解,不会执行环绕增强中的代码。
@Transactional
有关符号问题
Spring AOP支持如下三种通配符:
1、*
:匹配任何数量字符,用于参数列表表示参数可以是任意数据类型,但是必须有参数,例子:
java.*.Date——>匹配java包的下一级子包中的任何Date类型;如匹配java.util.Date、java.sql.Date,但不匹配java.util.sql.Date;
java.lang.*e——>匹配任何java.lang包下的以e结尾的类型,如匹配java.util.Hashtable、java.util.Date等等;
2、..
:方法中表示任意数量参数,在包中表示当前包及其子包,例子:
java…*——>匹配java包及其任何子包下的任何类型,如匹配java.lang.String、java.lang.annotation.Annotation等等;
3、+
:匹配指定类型的子类型(不是子类);仅能作为后缀放在类型模式后边,例子:
java.lang.Number+——>匹配java.lang包下任何Number的子类型,如匹配java.lang.Integer、java.math.BigInteger等等;
java.util.List+——>匹配java.util.List接口实现类,如匹配java.util.ArrayList,但不匹配java.util.HashMap