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

作者:小小工匠 时间:2022-05-31 16:08:38 

概述

使用this()或target()可绑定被代理对象实例,在通过类实例名绑定对象时,还依然具有原来连接点匹配的功能,只不过类名是通过增强方法中同名入参的类型间接决定罢了。

这里我们通过this()来了解对象绑定的用法:

实例

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

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

业务类


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()----");
}
}

①处的切点表达式首先按类变量名查找②处增强方法的入参列表,进而获取类变量名对应的类为


com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BussinessLogicService

这样就知道了切点的定义为


this(com.xgj.aop.spring.advisor.aspectJAdvance.bindProxyObj.BussinessLogicService)

即所有代理对象为BussinessLogicService类的所有方法匹配该切点。

②处的增强方法通过bussinessLogicService入参绑定目标对象。

可见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()进行绑定。

来源:https://artisan.blog.csdn.net/article/details/77960199

标签:Spring,AOP,@AspectJ,代理对象
0
投稿

猜你喜欢

  • IDEA + Maven环境下的SSM框架整合及搭建过程

    2023-08-26 02:06:55
  • 浅析Java设计模式编程中的单例模式和简单工厂模式

    2021-10-13 15:27:54
  • JVM类加载机制原理及用法解析

    2021-08-17 00:22:29
  • Java四种权限修饰符知识点详解

    2023-11-11 06:12:59
  • JDK1.8下载、安装和环境配置超详细教程(最新最完整)

    2022-07-22 12:58:34
  • C#定时关闭窗体实例

    2023-07-09 22:53:25
  • SpringBoot集成MybatisPlus报错的解决方案

    2022-06-28 05:03:20
  • C# Winform实现进度条显示

    2023-09-14 15:47:51
  • 实现java简单的线程池

    2023-08-09 06:05:15
  • C++实现LeetCode(131.拆分回文串)

    2023-07-24 09:58:42
  • 老生常谈Java字符串进阶(必看篇)

    2023-11-12 21:19:35
  • Hibernate多对一单项关联

    2023-05-10 18:16:11
  • Mybatis动态调用表名和字段名的解决方法

    2022-03-18 16:54:14
  • java如何利用FastJSON、Gson、Jackson三种Json格式工具自定义时间序列化

    2023-04-01 07:25:00
  • Unity2D实现游戏回旋镖

    2022-07-16 07:13:50
  • Java数据结构之链表、栈、队列、树的实现方法示例

    2021-10-07 10:40:29
  • MyBatis插入Insert、InsertSelective的区别及使用心得

    2023-08-25 04:34:28
  • Java实现限定时间CountDownLatch并行场景

    2023-06-05 01:47:27
  • Java流程控制语句最全汇总(中篇)

    2023-11-10 23:52:50
  • Java SpringBoot启动指定profile的8种方式详解

    2023-07-31 21:48:03
  • asp之家 软件编程 m.aspxhome.com