javascript
Spring-AOP @AspectJ进阶之访问连接点信息
文章目錄
- 概述
- JoinPoint
- ProceedingJoinPoint
- 實(shí)例
概述
AspectJ使用org.aspectj.lang.JoinPoint接口表示目標(biāo)類連接點(diǎn)對(duì)象,如果是環(huán)繞增強(qiáng)時(shí),使用org.aspectj.lang.ProceedingJoinPoint表示連接點(diǎn)對(duì)象,該類是JoinPoint的子接口。
任何一個(gè)增強(qiáng)方法都可以通過(guò)將第一個(gè)入?yún)⒙暶鳛镴oinPoint訪問到連接點(diǎn)上下文的信息。
我們先來(lái)了解一下這兩個(gè)接口的主要方法:
JoinPoint
-
java.lang.Object[] getArgs():獲取連接點(diǎn)方法運(yùn)行時(shí)的入?yún)⒘斜?#xff1b;
-
Signature getSignature() :獲取連接點(diǎn)的方法簽名對(duì)象;
-
java.lang.Object getTarget() :獲取連接點(diǎn)所在的目標(biāo)對(duì)象;
-
java.lang.Object getThis() :獲取代理對(duì)象本身;
ProceedingJoinPoint
ProceedingJoinPoint繼承JoinPoint子接口,它新增了兩個(gè)用于執(zhí)行連接點(diǎn)方法的方法:
-
java.lang.Object proceed() :通過(guò)反射執(zhí)行目標(biāo)對(duì)象的連接點(diǎn)處的方法;
-
java.lang.Object proceed(java.lang.Object[] args) :通過(guò)反射執(zhí)行目標(biāo)對(duì)象連接點(diǎn)處的方法,不過(guò)使用新的入?yún)⑻鎿Q原來(lái)的入?yún)ⅰ?/p>
實(shí)例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
業(yè)務(wù)類
package com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint;import org.springframework.stereotype.Component;/*** * * @ClassName: LogicService* * @Description: @Component標(biāo)注的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標(biāo)注的切面* * @author: Mr.Yang* * @date: 2017年9月12日 上午1:10:40*/@Aspect public class JoinPointAspect {// ①環(huán)繞增強(qiáng)@Around("execution(* dealLogic(..)) && target(com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint.LogicService)")public void crossCodeCutting(ProceedingJoinPoint pjp) throws Throwable { // ②聲明連接點(diǎn)入?yún)?/span>System.out.println("-------ProceedingJoinPoint begin----------");// ③訪問連接點(diǎn)信息System.out.println("arg[0]:" + pjp.getArgs()[0]);System.out.println("signature:" + pjp.getTarget().getClass());// ④通過(guò)連接點(diǎn)執(zhí)行目標(biāo)對(duì)象的方法pjp.proceed();System.out.println("-------ProceedingJoinPoint end----------");} }在①處,我們聲明了一個(gè)環(huán)繞增強(qiáng),在②處增強(qiáng)方法的第一個(gè)入?yún)⒙暶鳛镻receedingJoinPoint類型(注意一定要在第一個(gè)位置),在③處,我們通過(guò)連接點(diǎn)對(duì)象pjp訪問連接點(diǎn)的信息。在④處,我們通過(guò)連接點(diǎn)調(diào)用目標(biāo)對(duì)象的方法
將業(yè)務(wù)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)掃描類包以及應(yīng)用注解定義的bean --> <context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint"/><!-- 基于@AspectJ切面的驅(qū)動(dòng)器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJAdvance.joinPoint.JoinPointAspect"/></beans>測(cè)試類
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");} }運(yùn)行結(jié)果
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----------總結(jié)
以上是生活随笔為你收集整理的Spring-AOP @AspectJ进阶之访问连接点信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-AOP @AspectJ进
- 下一篇: Spring-AOP @AspectJ进