首先要配置好xml文件
<!-- 指定要扫名的包-->
<context:component-scan base-package="spring.aop.aop"></context:component-scan>
<!-- 使AspectJ起着作用:自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
1. 返回通知
返回通知可以,必须程序执行完成,可以获取程序运行完成的结果
函数的代码
@Aspect
@Component
public class LoggingAspect {
/**
* 在方法正常结束后执行的代码,
* 返回的通知是可以访问方法的返回值!!!
*/
@AfterReturning(value = "execution(* spring.aop.aop.ArithmeticCalculator.*(..))",returning = "r")
public void afterReturning(JoinPoint joinPoint,Object r){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+methodName+" "+args+" result is " + r);
}
}
主函数
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("spring-aop.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) applicationContext.getBean("ArithmeticCalculatorImpl");
System.out.println("result: "+arithmeticCalculator.add(1,7));
}
运行的结果
2. 异常通知
当程序执行出现异常才会触发的通知,也可以指定触发特定异常的类型,只需要在接受的参收设定为特定的异常类型
函数代码
@Aspect
@Component
public class LoggingAspect {
/**
* 在目标代码出现异常时会执行代码
* 可以访问到异常对象
* 可以指定出现异常时在执行通知代码
*/
@AfterThrowing(value = "execution(* spring.aop.aop.ArithmeticCalculatorImpl.*(..))",throwing = "exception")
public void afterThrowing(JoinPoint joinPoint,Exception exception){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" exception is "+exception);
}
}
主函数
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("spring-aop.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) applicationContext.getBean("ArithmeticCalculatorImpl");
System.out.println("result: "+arithmeticCalculator.div(1,0));
}
运行结果
3.环绕通知
环绕通知,类似于动态代理,在不同的地方调用函数,就类似于不同的通知
@Aspect
@Component
public class LoggingAspect {
/**
* 环绕通知需要携带ProceedingJoinPoint类型的参数
* 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行该目标方法
* 环绕通知必须有返回值,返回值即为目标方法的返回值
*/
@Around("execution(* spring.aop.aop.ArithmeticCalculatorImpl.*(..))")
public Object around(ProceedingJoinPoint proceedingJoinPoint){
System.out.println("Around");
Object result = null;
String methodName = proceedingJoinPoint.getSignature().getName();
List<Object> args = Arrays.asList(proceedingJoinPoint.getArgs());
try {
// 前置通知
System.out.println("The method "+methodName+" begins with "+args);
result = proceedingJoinPoint.proceed();
// 返回通知
System.out.println("The method "+methodName+" "+args+" result is " + result);
} catch (Throwable throwable) {
// 异常通知
System.out.println("The method "+methodName+" exception is "+throwable);
}
// 后置通知
System.out.println("The method "+methodName+" ends");
return result;
}
}
主函数
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("spring-aop.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) applicationContext.getBean("ArithmeticCalculatorImpl");
System.out.println("result: "+arithmeticCalculator.add(1,7));
}
运行结果