Spring-AOP @AspectJ进阶之访问连接点信息
文章目錄
- 概述
- 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进阶之访问连接点信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-AOP @AspectJ进
- 下一篇: Spring-AOP @AspectJ进