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

相关文章
|
存储 Java Spring
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
425 12
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
558 12
|
XML 安全 Java
Spring Boot中使用MapStruct进行对象映射
本文介绍如何在Spring Boot项目中使用MapStruct进行对象映射,探讨其性能高效、类型安全及易于集成等优势,并详细说明添加MapStruct依赖的步骤。
583 0
|
存储 缓存 Java
Spring高手之路23——AOP触发机制与代理逻辑的执行
本篇文章深入解析了Spring AOP代理的触发机制和执行流程,从源码角度详细讲解了Bean如何被AOP代理,包括代理对象的创建、配置与执行逻辑,帮助读者全面掌握Spring AOP的核心技术。
396 3
Spring高手之路23——AOP触发机制与代理逻辑的执行
|
Java Spring
获取spring工厂中bean对象的两种方式
获取spring工厂中bean对象的两种方式
233 1
|
前端开发 Java Spring
【Spring】“请求“ 之传递单个参数、传递多个参数和传递对象
【Spring】“请求“ 之传递单个参数、传递多个参数和传递对象
484 2
|
存储 Java 程序员
SpringIOC和DI的代码实现,Spring如何存取对象?@Controller、@Service、@Repository、@Component、@Configuration、@Bean DI详解
本文详细讲解了Spring框架中IOC容器如何存储和取出Bean对象,包括五大类注解(@Controller、@Service、@Repository、@Component、@Configuration)和方法注解@Bean的用法,以及DI(依赖注入)的三种注入方式:属性注入、构造方法注入和Setter注入,并分析了它们的优缺点。
334 0
SpringIOC和DI的代码实现,Spring如何存取对象?@Controller、@Service、@Repository、@Component、@Configuration、@Bean DI详解
|
6月前
|
XML Java 数据格式
《深入理解Spring》:AOP面向切面编程深度解析
Spring AOP通过代理模式实现面向切面编程,将日志、事务等横切关注点与业务逻辑分离。支持注解、XML和编程式配置,提供五种通知类型及丰富切点表达式,助力构建高内聚、低耦合的可维护系统。
|
6月前
|
监控 Java Spring
AOP 切面编程
AOP(面向切面编程)通过动态代理在不修改源码的前提下,对方法进行增强。核心概念包括连接点、通知、切入点、切面和目标对象。常用于日志记录、权限校验、性能监控等场景,结合Spring AOP与@Aspect、@Pointcut等注解,实现灵活的横切逻辑管理。
1688 6
AOP 切面编程

热门文章

最新文章

下一篇
开通oss服务