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

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

概述


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

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


实例


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


aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcwOTEzMDE0ODUxNzgz.png

业务类

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

相关文章
|
23天前
|
前端开发 Java Spring
Spring MVC 是如何对对象参数进行校验的
【6月更文挑战第4天】对象参数校验是使用 SpringMVC 时常用的功能,这篇文章尝试分析了,Spring 是如何实现这一功能的。
31 5
|
2月前
|
安全 Java 开发者
深入理解Spring Boot配置绑定及其实战应用
【4月更文挑战第10天】本文详细探讨了Spring Boot中配置绑定的核心概念,并结合实战示例,展示了如何在项目中有效地使用这些技术来管理和绑定配置属性。
30 1
|
7天前
|
运维 Java 关系型数据库
Spring运维之boot项目bean属性的绑定读取与校验
Spring运维之boot项目bean属性的绑定读取与校验
13 2
|
13天前
|
存储 XML Java
在 Java 中,Spring 框架提供了一种更加简单的方式来读取和存储对象
【6月更文挑战第18天】Java Spring 框架利用注解简化对象管理:@Component(及衍生注解@Service等)标注Bean类,自动注册到容器;@Autowired用于字段或方法,实现依赖注入,提升灵活性,减少XML配置。
17 2
|
21天前
|
JSON 前端开发 Java
Spring MVC 级联对象参数校验
【6月更文挑战第6天】在 Spring MVC 的使用过程中,我们会发现很多非常符合直觉的功能特性,但往往我们会习惯这种「被照顾得很好」的开发方式,依靠直觉去判断很多功能特性的用法。
19 1
|
2月前
|
存储 Java 对象存储
Spring 更简单的读取和存储对象
Spring 更简单的读取和存储对象
|
2月前
|
存储 XML Java
Spring框架学习 -- 读取和存储Bean对象
Spring框架学习 -- 读取和存储Bean对象
22 0
Spring框架学习 -- 读取和存储Bean对象
|
2月前
|
存储 Java 数据库
Spring的使用-Bean对象的储存和获取/Bea对象的作用域与生命周期
Spring的使用-Bean对象的储存和获取/Bea对象的作用域与生命周期
|
2月前
|
SQL Java 数据库连接
15:MyBatis对象关系与映射结构-Java Spring
15:MyBatis对象关系与映射结构-Java Spring
50 4
|
2月前
|
Java Spring
Spring⼯⼚创建复杂对象
Spring⼯⼚创建复杂对象
39 11