直接看代码:

package com.cn.spring.aop.impl;
//加减乘除的接口类
public interface ArithmeticCalculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}
package com.cn.spring.aop.impl;

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;
}
}
package com.cn.spring.aop.impl;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import java.util.Arrays;
import java.util.List; //把这个类声明为一个切面:首先需要把该类放入到IOC容器中,在声明为一个切面
//可以使用@order注解指定切面的优先级,值越小,优先级越高
@Order(2)
@Aspect
@Component
public class LoggingAspect { /**
* 定义一个方法,用于声明切入点表达式,一般的,该方法中再不需要添入其他的代码
* 使用@Pointcut来声明切入点表达式
* 后面的其他通知直接使用方法名来引用当前的切入点表达式
*/
@Pointcut(value = "execution(public int ArithmeticCalculator.*(..))")
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 method " + methodName + " begins with " + args);
} //后置通知:在目标方法执行后(无论是否发生异常),执行的通知
//在后置通知中还不能访问目标方法执行的结果
@After("declareJoinPointExpression()")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " ends with " + args);
} /**
* 在方法正常结束后执行的代码
* 返回通知是可以访问到方法的返回值
* @param joinPoint
*/
@AfterReturning(value = "declareJoinPointExpression()",
returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method ends witd " + result);
} //在目标方法出现异常时会执行的代码
//可以访问到异常对象;且可以指定在出现特定异常时再执行通知代码
@AfterThrowing(value = "declareJoinPointExpression()",
throwing = "ex")
public void afterReturning(JoinPoint joinPoint, Exception ex) {
String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " occures exception with: " + ex);
} /**
* 环绕通知需要携带ProceedingJoinPoint类型的参数
* 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法
* 且环绕通知必须有返回值,返回值为目标方法的返回值
* @param proceedingJoinPoint
*/
@Around("declareJoinPointExpression()")
public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) {
Object result = null;
String methodName = proceedingJoinPoint.getSignature().getName();
//执行目标方法
try {
//前置通知
System.out.println("The method " + methodName + " begins with " + Arrays.asList(proceedingJoinPoint.getArgs()));
result = proceedingJoinPoint.proceed();
//返回通知
System.out.println("The method ends with " + result);
} catch (Throwable throwable) {
//异常通知
System.out.println("The method " + methodName + " occures exception with: " + throwable);
throw new RuntimeException(throwable);
}
//后置通知
System.out.println("The method " + methodName + " ends");
return result;
}
}
package com.cn.spring.aop.impl;

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; import java.util.Arrays; @Order(1)
@Aspect
@Component
public class ValidationAspect { @Before("LoggingAspect.declareJoinPointExpression()")//重用切点表达式
public void validateArgs(JoinPoint joinPoint) {
System.out.println("validate:" + Arrays.asList(joinPoint.getArgs()));
}
}
package com.cn.spring.aop.impl;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
//1.创建Spring的IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("17-1.xml"); //2.从IOC容器中huo获取bean的实例
ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class); //3.使用bean
int result = arithmeticCalculator.add(3, 6); System.out.println("result:" + result);
//result = arithmeticCalculator.div(3, 0);
// System.out.println("result:" + 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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.cn.spring.aop.impl">
</context:component-scan> <!--使AspjectJ注解起作用:自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

最新文章

  1. ubuntu下部署SVN
  2. android摇一摇实现(仿微信)
  3. 21.2 Partitioning Types
  4. OPENGL若干重要基础概念
  5. JavaWeb监听器详解
  6. 用UNIX消息队列实现IPC(以ATM为例)
  7. hdu 5091 Beam Cannon
  8. 屏幕分辨率与FPS
  9. C语言控制语句总结(if else for switch while break continue)
  10. JAVA-4-斐波列
  11. View Controller 生命周期的各个方法的用法
  12. ubuntu下配置qt+opengl+opencv
  13. ANSI C中取得结构体字段偏移量的常用方法
  14. HDU Today (图论)
  15. 如何深入系统的学习一门编程语言——python自学笔记
  16. Linux下执行Oracle的sql脚本
  17. iptables转发端口
  18. Flutter基础用法解析
  19. linux之docker学习
  20. Android-Kotlin-抽象类与多态的表现

热门文章

  1. Git学习三
  2. Unix\Linux | 总结笔记 | 命令_ WC
  3. [POI2008]POD Subdivision of Kingdom
  4. 题解报告:hdu 1541 Stars(经典BIT)
  5. 一个小方法解决RGBA不兼容IE8
  6. iOS Programming Camera 2
  7. win8怎么打开或关闭快速启动(进入BIOS前的设置)
  8. CentOS 7 samba server 配置
  9. Swift - 值类型与引用类型的初步探究
  10. CREATE TRIGGER - 定义一个新的触发器