基于@AspectJ的AOP的使用
来源:互联网 发布:白俄罗斯军力知乎 编辑:程序博客网 时间:2024/05/30 23:02
1.添加Maven依赖
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version></dependency>
2.启用对@AspectJ的支持
Spring默认不支持@AspectJ风格的切面声明,为了支持需要使用如下配置:
<!-- proxy-target-class 默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy poxy-target-class="true"/>时,表示使用CGLib动态代理技术织入增强 --><aop:aspectj-autoproxy proxy-target-class="true"/>
3.声明切面
@AspectJ风格的声明切面非常简单,使用@Aspect注解进行声明:
@Aspectpublic class MyAspect {}
4.声明切点
@AspectJ风格的命名切入点使用org.aspectj.lang.annotation包下的@Pointcut+方法(方法必须是返回void类型)实现
/** * 定义切点方法 没有实际的意义 */ @Pointcut(value = "execution(* com.platform.service.LoginService.*(..))") public void myPointCut(){ }
value:指定切入点表达式;
argNames:指定命名切入点方法参数列表参数名字,可以有多个用“,”分隔,这些参数将传递给通知方法同名的参数,同时比如切入点表达式“args(param)”将匹配参数类型为命名切入点方法同名参数指定的参数类型。
pointcutName:切入点名字,可以使用该名字进行引用该切入点表达式
5.声明通知
@AspectJ风格的声明通知也支持5种通知类型:
- 前置通知:使用org.aspectj.lang.annotation 包下的@Before注解声明
@Before("myPointCut()") public void authority(JoinPoint jp){ System.out.println("before 增强 进行权限处理"); System.out.println("处理的目标方法为:"+jp.getSignature().getName()); System.out.println("目标方法的参数为:"+Arrays.toString(jp.getArgs())); System.out.println("目标方法的对象为:"+jp.getTarget()); }
- 后置返回通知:使用org.aspectj.lang.annotation 包下的@AfterReturning注解声明
@AfterReturning(pointcut="myPointCut()",returning="rvt") public void doLog(JoinPoint jp,Object rvt){ System.out.println("@AfterReturning 增强 进行日志记录 返回值为:"+rvt); System.out.println("处理的目标方法为:"+jp.getSignature().getName()); System.out.println("目标方法的参数为:"+Arrays.toString(jp.getArgs())); System.out.println("目标方法的对象为:"+jp.getTarget()); }
value:指定切入点表达式或命名切入点;
pointcut:同样是指定切入点表达式或命名切入点,如果指定了将覆盖 value属性指定的,pointcut具有高优先级;
argNames:与Schema方式配置中的同义;
returning:与Schema方式配置中的同义
- 后置异常通知:使用org.aspectj.lang.annotation 包下的@AfterThrowing注解声明;
@AfterThrowing(pointcut="myPointCut()",argNames="exception", throwing="exception") public void afterThrowing(JoinPoint jp,Exception exception){ System.out.println("@AfterThrowing 异常处理"); System.out.println("处理的目标方法为:"+jp.getSignature().getName()); System.out.println("目标方法的参数为:"+Arrays.toString(jp.getArgs())); System.out.println("目标方法的对象为:"+jp.getTarget()); System.out.println("异常为:"+exception.getMessage()); }
- 后置最终通知:使用org.aspectj.lang.annotation 包下的
@After("myPointCut()") public void release(JoinPoint jp){ System.out.println("@After 增强 进行权限处理"); System.out.println("处理的目标方法为:"+jp.getSignature().getName()); System.out.println("目标方法的参数为:"+Arrays.toString(jp.getArgs())); System.out.println("目标方法的对象为:"+jp.getTarget()); }
- 环绕通知:使用org.aspectj.lang.annotation 包下的@Around注解声明;
@Around("myPointCut()") public Object processTx(ProceedingJoinPoint jp) throws Throwable{ System.out.println("Around 增强 执行目标方法之前"); Object[] args = jp.getArgs(); if(args != null && args.length > 0 & args[0].getClass() == String.class){ args[0] = "被改变的参数"; } Object rvt = jp.proceed(args); System.out.println("执行目标方法只会,事务结束"); return rvt; }
value:指定切入点表达式或命名切入点;
argNames:与Schema方式配置中的同义;
阅读全文
1 0
- 基于@AspectJ的AOP
- 基于@AspectJ的AOP
- 基于@AspectJ的AOP的使用
- 使用AspectJ基于注解的Aop
- 【Spring AOP】基于AspectJ的AOP基本使用
- 基于aspectj的注解aop
- 基于aspectj的aop操作
- Spring-AOP:基于AspectJ注解的AOP
- Spring AOP的使用 基于全注解AspectJ
- Spring的基于AspectJ的AOP开发
- Spring基于annotation的AOP(AspectJ)
- Spring基于注解@AspectJ的AOP
- Spring基于注解@AspectJ的AOP
- Spring详解------------基于@ASpectJ的AOP
- 6.基于@AspectJ和Schema的AOP
- spring基于AspectJ的AOP编程
- day3_Spring_02_基于aspectj的注解aop操作
- 9.4.Spring 中基于 AOP 的 @AspectJ
- Python基本数据类型
- 动态加载总结
- 为什么jsonp只支持get请求?
- ACM的奇技淫巧-输出外挂(相关博客)
- 类加载器ClassLoader
- 基于@AspectJ的AOP的使用
- JavaFX制作一个简单的时钟
- 服务器架设
- HDFS读文件过程分析:读取文件的Block数据
- nginx在Centos下的安装,转:http://www.linuxidc.com/Linux/2016-09/134907.htm
- python爬虫练习5:博客阅读量助手
- 初识hibernate
- Ubuntu Windows双系统引导的问题
- js之script属性async与defer