Spring-AOP @AspectJ進(jìn)階之如何綁定代理對(duì)象
概述
使用this()或target()可綁定被代理對(duì)象實(shí)例,在通過(guò)類(lèi)實(shí)例名綁定對(duì)象時(shí),還依然具有原來(lái)連接點(diǎn)匹配的功能,只不過(guò)類(lèi)名是通過(guò)增強(qiáng)方法中同名入?yún)⒌念?lèi)型間接決定罷了。
這里我們通過(guò)this()來(lái)了解對(duì)象綁定的用法:
實(shí)例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster

業(yè)務(wù)類(lèi)
package com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj;
import org.springframework.stereotype.Component;
/**
*
*
* @ClassName: BussinessLogicService
*
* @Description: @Component標(biāo)注的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: 綁定代理對(duì)象
* 使用this()或target()可綁定被代理對(duì)象實(shí)例,在通過(guò)類(lèi)實(shí)例名綁定對(duì)象時(shí),還依然具有原來(lái)連接點(diǎn)匹配的功能,
* 只不過(guò)類(lèi)名是通過(guò)增強(qiáng)方法中同名入?yún)⒌念?lèi)型間接決定罷了
*
* @author: Mr.Yang
*
* @date: 2017年9月12日 下午12:04:44
*/
@Aspect
public class BindProxyObjAspect {
// (1)處通過(guò)②處查找出waiter對(duì)應(yīng)的類(lèi)型為BussinessLogicService,因而切點(diǎn)表達(dá)式
// 為this(bussinessLogicService),當(dāng)增強(qiáng)方法織入目標(biāo)連接點(diǎn)時(shí),增強(qiáng)方法通過(guò)bussinessLogicService
// 入?yún)⒖梢砸玫酱韺?duì)象的實(shí)例。
@Before("this(bussinessLogicService)")
public void bindProxyObj(BussinessLogicService bussinessLogicService) { // (2)
System.out.println("----bindProxyObj()----");
System.out.println(bussinessLogicService.getClass().getName());
System.out.println("----bindProxyObj()----");
}
}
①處的切點(diǎn)表達(dá)式首先按類(lèi)變量名查找②處增強(qiáng)方法的入?yún)⒘斜?,進(jìn)而獲取類(lèi)變量名對(duì)應(yīng)的類(lèi)為
com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BussinessLogicService
這樣就知道了切點(diǎn)的定義為
this(com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BussinessLogicService)
即所有代理對(duì)象為BussinessLogicService類(lèi)的所有方法匹配該切點(diǎn)。
②處的增強(qiáng)方法通過(guò)bussinessLogicService入?yún)⒔壎繕?biāo)對(duì)象。
可見(jiàn)BussinessLogicService的所有方法匹配①處的切點(diǎn)
配置文件
<?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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- (1)聲明Context命名空間以及Schema文件 (2)掃描類(lèi)包以及應(yīng)用注解定義的bean -->
<context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj"/>
<!-- 基于@AspectJ切面的驅(qū)動(dòng)器 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 使用了@AspectJ注解的切面類(lèi) -->
<bean class="com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BindProxyObjAspect"/>
</beans>
測(cè)試類(lèi)
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 {
@Test
public 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();
}
}
運(yùn)行結(jié)果
2017-09-12 13:54:41,463 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@292898f5: startup date [Tue Sep 12 13:54:41 BOT 2017]; root of context hierarchy
2017-09-12 13:54:41,557 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()進(jìn)行綁定。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 如何使用Spring AOP預(yù)處理Controller的參數(shù)
- 基于Spring AOP proxyTargetClass的行為表現(xiàn)總結(jié)
- SpringAOP如何獲取方法參數(shù)上的注解
- SpringBoot Aop 詳解和多種使用場(chǎng)景解析
- Springboot+AOP實(shí)現(xiàn)返回?cái)?shù)據(jù)提示語(yǔ)國(guó)際化的示例代碼
- spring-AOP 及 AOP獲取request各項(xiàng)參數(shù)操作
- Spring Aop 如何獲取參數(shù)名參數(shù)值
- 聊聊Spring——AOP詳解(AOP概覽)
相關(guān)文章
SpringBoot項(xiàng)目打包三方JAR的示例代碼
本篇文章主要介紹了SpringBoot項(xiàng)目打包三方JAR的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
MyBatis-Plus動(dòng)態(tài)表名使用selectPage方法不生效問(wèn)題解析與解決方案
MyBatis-Plus是MyBatis的增強(qiáng)工具,動(dòng)態(tài)表名是MyBatis-Plus的一個(gè)重要功能之一,一些開(kāi)發(fā)者在使用selectPage方法時(shí)可能會(huì)遇到動(dòng)態(tài)表名不生效的問(wèn)題,本文將深入分析這個(gè)問(wèn)題的原因,并提供相應(yīng)的解決方案,需要的朋友可以參考下2023-12-12
springboot集成mybatisplus實(shí)例詳解
這篇文章主要介紹了springboot集成mybatisplus實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Java設(shè)計(jì)模式之創(chuàng)建者模式簡(jiǎn)介
這篇文章主要介紹了Java設(shè)計(jì)模式之創(chuàng)建者模式,需要的朋友可以參考下2014-07-07
JavaEE實(shí)現(xiàn)前后臺(tái)交互的文件上傳與下載
這篇文章主要介紹了JavaEE實(shí)現(xiàn)前后臺(tái)交互的文件上傳與下載,分享相關(guān)技術(shù),實(shí)現(xiàn)文件上傳下載功能,需要的朋友可以參考下2015-11-11
Spring使用@Filter注解創(chuàng)建自定義過(guò)濾器
Spring 中鮮為人知但非常有用的注解之一是 @Filter,它支持自定義過(guò)濾器,下面我們就來(lái)深入研究一下如何使用 Spring 的 @Filter 注解來(lái)創(chuàng)建自定義過(guò)濾器吧2023-11-11
Java中的FileInputStream 和 FileOutputStream 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
FileInputStream 是文件輸入流,它繼承于InputStream。FileOutputStream 是文件輸出流,它繼承于OutputStream。接下來(lái)通過(guò)本文給大家介紹Java中的FileInputStream 和 FileOutputStream,需要的朋友可以參考下2017-05-05
詳解使用spring boot admin監(jiān)控spring cloud應(yīng)用程序
這篇文章主要介紹了詳解使用spring boot admin監(jiān)控spring cloud應(yīng)用程序,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

