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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring-AOP @AspectJ进阶之绑定代理对象

發布時間:2025/3/21 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring-AOP @AspectJ进阶之绑定代理对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  • 概述
  • 實例

概述

使用this()或target()可綁定被代理對象實例,在通過類實例名綁定對象時,還依然具有原來連接點匹配的功能,只不過類名是通過增強方法中同名入參的類型間接決定罷了。

這里我們通過this()來了解對象綁定的用法:

實例

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


業務類

package com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj;import org.springframework.stereotype.Component;/*** * * @ClassName: BussinessLogicService* * @Description: @Component標注的bean* * @author: Mr.Yang* * @date: 2017年9月12日 下午12:11:28*/@Component public class BussinessLogicService {public void doLogic() {System.out.println("BussinessLogicService doLogic executed ");} }

切面

package com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;/*** * * @ClassName: BindProxyObjAspect* * @Description: 綁定代理對象* 使用this()或target()可綁定被代理對象實例,在通過類實例名綁定對象時,還依然具有原來連接點匹配的功能,* 只不過類名是通過增強方法中同名入參的類型間接決定罷了* * @author: Mr.Yang* * @date: 2017年9月12日 下午12:04:44*/@Aspect public class BindProxyObjAspect {// (1)處通過②處查找出waiter對應的類型為BussinessLogicService,因而切點表達式// 為this(bussinessLogicService),當增強方法織入目標連接點時,增強方法通過bussinessLogicService// 入參可以引用到代理對象的實例。@Before("this(bussinessLogicService)")public void bindProxyObj(BussinessLogicService bussinessLogicService) { // (2)System.out.println("----bindProxyObj()----");System.out.println(bussinessLogicService.getClass().getName());System.out.println("----bindProxyObj()----");}}

(1)處的切點表達式首先按類變量名查找(2)處增強方法的入參列表,進而獲取類變量名對應的類為com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BussinessLogicService,這樣就知道了切點的定義為this(com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BussinessLogicService),即所有代理對象為BussinessLogicService類的所有方法匹配該切點。
(2)處的增強方法通過bussinessLogicService入參綁定目標對象。
可見BussinessLogicService的所有方法匹配(1)處的切點


配置文件

<?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.bindProxyObj"/><!-- 基于@AspectJ切面的驅動器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BindProxyObjAspect"/></beans>

測試類

package com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class BindProxyObjAspectTest {@Testpublic void test() {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/aspectJAdvance/bindProxyObj/conf-bindProxyObj.xml");BussinessLogicService bussinessLogicService = ctx.getBean("bussinessLogicService", BussinessLogicService.class);bussinessLogicService.doLogic();} }

運行結果

2017-09-13 11:46:49,559 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3695de1a: startup date [Wed Sep 13 11:46:49 BOT 2017]; root of context hierarchy 2017-09-13 11:46:49,652 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJAdvance/bindProxyObj/conf-bindProxyObj.xml] ----bindProxyObj()---- com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BussinessLogicService$$EnhancerBySpringCGLIB$$472f5f0d ----bindProxyObj()---- BussinessLogicService doLogic executed

按相似的方法使用target()進行綁定。

總結

以上是生活随笔為你收集整理的Spring-AOP @AspectJ进阶之绑定代理对象的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。