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

業(yè)務類
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()可綁定被代理對象實例,在通過類實例名綁定對象時,還依然具有原來連接點匹配的功能,
* 只不過類名是通過增強方法中同名入?yún)⒌念愋烷g接決定罷了
*
* @author: Mr.Yang
*
* @date: 2017年9月12日 下午12:04:44
*/
@Aspect
public class BindProxyObjAspect {
// (1)處通過②處查找出waiter對應的類型為BussinessLogicService,因而切點表達式
// 為this(bussinessLogicService),當增強方法織入目標連接點時,增強方法通過bussinessLogicService
// 入?yún)⒖梢砸玫酱韺ο蟮膶嵗?
@Before("this(bussinessLogicService)")
public void bindProxyObj(BussinessLogicService bussinessLogicService) { // (2)
System.out.println("----bindProxyObj()----");
System.out.println(bussinessLogicService.getClass().getName());
System.out.println("----bindProxyObj()----");
}
}
①處的切點表達式首先按類變量名查找②處增強方法的入?yún)⒘斜?,進而獲取類變量名對應的類為
com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BussinessLogicService
這樣就知道了切點的定義為
this(com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BussinessLogicService)
即所有代理對象為BussinessLogicService類的所有方法匹配該切點。
②處的增強方法通過bussinessLogicService入?yún)⒔壎繕藢ο蟆?/p>
可見BussinessLogicService的所有方法匹配①處的切點
配置文件
<?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)掃描類包以及應用注解定義的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 {
@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();
}
}
運行結果
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īng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
MyBatis-Plus動態(tài)表名使用selectPage方法不生效問題解析與解決方案
MyBatis-Plus是MyBatis的增強工具,動態(tài)表名是MyBatis-Plus的一個重要功能之一,一些開發(fā)者在使用selectPage方法時可能會遇到動態(tài)表名不生效的問題,本文將深入分析這個問題的原因,并提供相應的解決方案,需要的朋友可以參考下2023-12-12
Spring使用@Filter注解創(chuàng)建自定義過濾器
Spring 中鮮為人知但非常有用的注解之一是 @Filter,它支持自定義過濾器,下面我們就來深入研究一下如何使用 Spring 的 @Filter 注解來創(chuàng)建自定義過濾器吧2023-11-11
Java中的FileInputStream 和 FileOutputStream 介紹_動力節(jié)點Java學院整理
FileInputStream 是文件輸入流,它繼承于InputStream。FileOutputStream 是文件輸出流,它繼承于OutputStream。接下來通過本文給大家介紹Java中的FileInputStream 和 FileOutputStream,需要的朋友可以參考下2017-05-05
詳解使用spring boot admin監(jiān)控spring cloud應用程序
這篇文章主要介紹了詳解使用spring boot admin監(jiān)控spring cloud應用程序,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05

