日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring-AOP @AspectJ进阶之访问连接点信息

發(fā)布時(shí)間:2025/3/21 javascript 69 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring-AOP @AspectJ进阶之访问连接点信息 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 概述
    • 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訪問(wèn)到連接點(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);} }

編寫(xiě)切面

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----------");// ③訪問(wèn)連接點(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訪問(wèn)連接點(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)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。