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

業(yè)務(wù)類
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ò)類實(shí)例名綁定對(duì)象時(shí),還依然具有原來(lái)連接點(diǎn)匹配的功能,
* 只不過(guò)類名是通過(guò)增強(qiáng)方法中同名入?yún)⒌念愋烷g接決定罷了
*
* @author: Mr.Yang
*
* @date: 2017年9月12日 下午12:04:44
*/
@Aspect
public class BindProxyObjAspect {
// (1)處通過(guò)②處查找出waiter對(duì)應(yīng)的類型為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á)式首先按類變量名查找②處增強(qiáng)方法的入?yún)⒘斜恚M(jìn)而獲取類變量名對(duì)應(yīng)的類為
com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BussinessLogicService
這樣就知道了切點(diǎn)的定義為
this(com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BussinessLogicService)
即所有代理對(duì)象為BussinessLogicService類的所有方法匹配該切點(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)掃描類包以及應(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注解的切面類 -->
<bean class="com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BindProxyObjAspect"/>
</beans>
測(cè)試類
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)文章
利用Mybatis向PostgreSQL中插入并查詢JSON字段
這篇文章主要介紹了利用Mybatis向PostgreSQL中插入并查詢JSON字段,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07
MyBatis-Plus 使用枚舉自動(dòng)關(guān)聯(lián)注入
本文主要介紹了MyBatis-Plus 使用枚舉自動(dòng)關(guān)聯(lián)注入,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
Java實(shí)現(xiàn)根據(jù)前端所要格式返回樹(shù)形3級(jí)層級(jí)數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)根據(jù)前端所要格式返回樹(shù)形3級(jí)層級(jí)數(shù)據(jù),文中的示例代碼講解詳細(xì),有需要的小伙伴可以了解下2024-02-02
學(xué)生視角看Java 面向?qū)ο蟮睦^承本質(zhì)
繼承是java面向?qū)ο缶幊碳夹g(shù)的一塊基石,因?yàn)樗试S創(chuàng)建分等級(jí)層次的類。繼承就是子類繼承父類的特征和行為,使得子類對(duì)象(實(shí)例)具有父類的實(shí)例域和方法,或子類從父類繼承方法,使得子類具有父類相同的行為2022-03-03
Mybatis操作多數(shù)據(jù)源的實(shí)現(xiàn)
本文主要介紹了Mybatis操作多數(shù)據(jù)源,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
IDEA插件開(kāi)發(fā)之環(huán)境搭建過(guò)程圖文詳解
這篇文章主要介紹了IDEA插件開(kāi)發(fā)之環(huán)境搭建過(guò)程,本文通過(guò)圖文并茂實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Java spring webmvc如何實(shí)現(xiàn)控制反轉(zhuǎn)
這篇文章主要介紹了Java spring webmvc如何實(shí)現(xiàn)控制反轉(zhuǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08

