文章目录

  • 概述
    • JoinPoint
    • ProceedingJoinPoint
  • 实例

概述

AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点对象,该类是JoinPoint的子接口。

任何一个增强方法都可以通过将第一个入参声明为JoinPoint访问到连接点上下文的信息。

我们先来了解一下这两个接口的主要方法:

JoinPoint

  • java.lang.Object[] getArgs():获取连接点方法运行时的入参列表;

  • Signature getSignature() :获取连接点的方法签名对象;

  • java.lang.Object getTarget() :获取连接点所在的目标对象;

  • java.lang.Object getThis() :获取代理对象本身;


ProceedingJoinPoint

ProceedingJoinPoint继承JoinPoint子接口,它新增了两个用于执行连接点方法的方法:

  • java.lang.Object proceed() :通过反射执行目标对象的连接点处的方法;

  • java.lang.Object proceed(java.lang.Object[] args) :通过反射执行目标对象连接点处的方法,不过使用新的入参替换原来的入参。


实例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster


业务类

package com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint;import org.springframework.stereotype.Component;/*** * * @ClassName: LogicService* * @Description: @Component标注的Bean* * @author: Mr.Yang* * @date: 2017年9月12日 上午1:09:38*/@Component
public class LogicService {public void dealLogic(String bussiness) {System.out.println("deal Logic:" + bussiness);}
}

编写切面

package com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;/*** * * @ClassName: JoinPointAspect* * @Description: @Aspect标注的切面* * @author: Mr.Yang* * @date: 2017年9月12日 上午1:10:40*/@Aspect
public class JoinPointAspect {// ①环绕增强@Around("execution(* dealLogic(..)) && target(com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint.LogicService)")public void crossCodeCutting(ProceedingJoinPoint pjp) throws Throwable { // ②声明连接点入参System.out.println("-------ProceedingJoinPoint begin----------");// ③访问连接点信息System.out.println("arg[0]:" + pjp.getArgs()[0]);System.out.println("signature:" + pjp.getTarget().getClass());// ④通过连接点执行目标对象的方法pjp.proceed();System.out.println("-------ProceedingJoinPoint end----------");}
}

在①处,我们声明了一个环绕增强,在②处增强方法的第一个入参声明为PreceedingJoinPoint类型(注意一定要在第一个位置),在③处,我们通过连接点对象pjp访问连接点的信息。在④处,我们通过连接点调用目标对象的方法

将业务Bean和切面配置到配置文件中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)声明Context命名空间以及Schema文件   (2)扫描类包以及应用注解定义的bean -->
<context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint"/><!-- 基于@AspectJ切面的驱动器 -->
<aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面类 -->
<bean class="com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint.JoinPointAspect"/></beans>

测试类

package com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class JoinPointAspectTest {@Testpublic void test() {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/aspectJAdvance/joinPoint/conf-joinPoint.xml");LogicService logicService = ctx.getBean("logicService",LogicService.class);logicService.dealLogic("PROGRAMMING");}
}

运行结果

2017-09-12 01:19:27,120  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@292898f5: startup date [Tue Sep 12 01:19:27 BOT 2017]; root of context hierarchy
2017-09-12 01:19:27,196  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJAdvance/joinPoint/conf-joinPoint.xml]
-------ProceedingJoinPoint begin----------
arg[0]:PROGRAMMING
signature:class com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint.LogicService
deal Logic:PROGRAMMING
-------ProceedingJoinPoint end----------

Spring-AOP @AspectJ进阶之访问连接点信息相关推荐

  1. 关于 Spring AOP (AspectJ) 你该知晓的一切

    [版权申明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/54629058 出自[zejian ...

  2. Spring AOP + AspectJ Annotation Example---reference

    In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...

  3. Spring AOP / AspectJ AOP 的区别?

    Spring AOP / AspectJ AOP 的区别? Spring AOP属于运行时增强,而AspectJ是编译时增强. Spring AOP基于代理(Proxying),而AspectJ基于字 ...

  4. java around_java - 使用Spring AOP时,在单个连接点上具有参数绑定的多个Around建议会导致错误 - 堆栈内存溢出...

    我在一个方法上写了2条注释,并在2条周围建议中处理了每个注释值. 连接点方法如下: @CacheFetch(cacheName = CacheManager.CACHE_DATASOURCE_INFO ...

  5. Spring AOP,AspectJ,CGLIB 有点晕

    AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,如事务管理.安全检查.缓存.对象池管理等.AOP 实现的关键就在于 ...

  6. Spring AOP AspectJ 代码实例

    本文参考来源 http://examples.javacodegeeks.com/enterprise-java/spring/aop/spring-aop-aspectj-example/ http ...

  7. Spring AOP AspectJ Pointcut Expressions With Examples--转

    原文地址:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with ...

  8. Spring AOP AspectJ

    本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代码. Pointcut:注入Advice的位置,切入点,一般为某 ...

  9. Spring-AOP @AspectJ进阶之绑定连接点方法入参

    文章目录 概述 实例 概述 我们前面的博文在讲解切点函数时说过args().this().target().@args().@within().@target()和@annotation()这7个函数 ...

最新文章

  1. 下一个十年,AI将在这10大领域颠覆世界!
  2. linux禁止root用户直接登录sshd并修改默认端口
  3. java 查询功能实现的八种方式
  4. dotConnect for Oracle控件免费下载及使用方法
  5. 草稿-乱-爬虫-post请求数据与Request Payload
  6. RabbitMQ和Kafka的显著差异(4)
  7. Android-JNI开发系列《十二》总结JNI知识体系
  8. ali 媒体转码签名生成-php
  9. oracle脱敏脚本
  10. 快速查看本机公网IP地址
  11. 免费的视频转Gif软件
  12. 利用FME对坐标文件进行坐标转换
  13. 【学习笔记】Baby Step Giant Step算法及其扩展
  14. Unparseable date: xxxxxx
  15. systemd服务详解
  16. 操作系统OS-Lab2-FAT12文件读取
  17. np.arange函数
  18. 安卓期末大作业Android studio-记单词app(资源链接在文末,含注册登录,含设计报告,含导出app文件及源码导入方法文档)
  19. matlab绘制分段函数,二维函数
  20. FPGA学习笔记_ROM核调用与调试

热门文章

  1. java src 文件路径_java中获得src路径下文件的常用方法
  2. mac电脑开机出现客人用户,没有输入密码框,其他键都点不动,重启也不好使
  3. 如何规划自己的博士五年生活?
  4. 可视化应用实战案例:绘制交互式+pdf+png等多格式桑基图
  5. 数据中台推荐系统入门(二):两种经典的推荐算法
  6. Windows下安装spark+Hadoop
  7. matlab R2010a windows和linux版本下载地址
  8. 抖音数据统计_“彭十六elf”单条视频获赞200W+,荣登抖音TOP20丨红人榜
  9. Python入门100题 | 第022题
  10. 开源项目成熟度分析工具-利用github api获取代码库的信息