问题描述, 如下Abc定义为一个Bean, b()方法添加@TargetDatasource,定义切面DynamicDataSourceAspect,期望:调用a()方法,b()方法上的AOP拦截能生效。实际不生效。

@Service

public class Abc {

public void a(){
b();
} @TargetDatasource(value=abc)
public void b(){
}
}

AOP代码:

@Order(-1)
@Component
@Slf4j
@Aspect
public class DynamicDataSourceAspect { /**
* set data source value.
*/
@Before(value = "@annotation(targetDataSource)", argNames = "targetDataSource")
public void setDataSource(TargetDataSource targetDataSource) {
if (targetDataSource != null && targetDataSource.value() != null) {
log.info("Databse value: {}", targetDataSource.value());
DynamicDataSourceHolder.set(targetDataSource.value());
}
} /**
* clear data source value.
*/
@After(value = "@annotation(targetDataSource)", argNames = "targetDataSource")
public void clearDataSource(TargetDataSource targetDataSource) {
DynamicDataSourceHolder.clear();
}
}

问题分析:

我们都知道Spring aop有两种实现方式,基于Interface生成代理和cglib生成代理。上面的例子中,当调用a()方法时,调用者拿到的是Abc的代理类,即增强类。如果a()方法上有@TargetDatasource注解,拦截会生效。然而,在a()方法里调用b(),b方法上的拦截不会生效。原因是因为a()调用b(), 用的是b()方法的目标类,而不是代理类,所以拦截不生效。

解决方法:

1. 重构代码, 把b()方法移到一个Bean里面。

2. 调整a()方法如下:

@Service

public class Abc {

public void a(){
((Abc)AopContext.currentProxy()).b();
} @TargetDatasource(value=abc)
public void b(){
}
}

Sping 官方解释 - https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop

Spring 嵌套方法AOP不生效问题的更多相关文章

  1. 使用spring框架进行aop编程实现方法调用前日志输出

    aop编程 之使用spring框架实现方法调用前日志输出 使用spring框架实现AOP编程首先需要搭建spring框架环境: 使用Spring框架实现AOP工程编程之后,不需要我们去写代理工厂了,工 ...

  2. Spring基础系列--AOP织入逻辑跟踪

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9619910.html 其实在之前的源码解读里面,关于织入的部分并没有说清楚,那些前置.后 ...

  3. Spring系列之AOP的原理及手动实现

    目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 引入 到目前为止,我们已经完成了简易的IOC和DI的功能,虽然相比如Spring来说肯定是非常简陋的,但是毕竟我 ...

  4. Spring基础系列-AOP源码分析

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9560803.html 一.概述 Spring的两大特性:IOC和AOP. AOP是面向切 ...

  5. spring IOC 和AOP 方面

    spring 的2大核心 是Ioc 和 aop  spring的依赖注入:在程序运行期间,由外部容器动态的将依赖对象注入到组件中  IOC: 实例化spring容器的二种方法 第一种:在类路径下寻找配 ...

  6. Spring IOC 和 AOP概述

    IoC(控制反转,(Inversion of Control):本来是由应用程序管理的对象之间的依赖关系,现在交给了容器管理,这就叫控制反转,即交给了IoC容器,Spring的IoC容器主要使用DI方 ...

  7. 4.spring:@Profile,AOP

    Profile: 可以根据当前的环境,动态激活和切换一系列的组件功能 指定组件在那个环境下才能被注册到容器中,不指定任何环境下都能注册到 1.加了环境标识的bean只有环境激活的时候才能注册到容器中 ...

  8. Spring事务管理—aop:pointcut expression 常见切入点表达式及事务说明

    Spring事务管理—aop:pointcut expression 常见切入点表达式及事物说明 例: <aop:config>  <aop:pointcut expression= ...

  9. Spring笔记:AOP基础

    Spring笔记:AOP基础 AOP 引入AOP 面向对象的开发过程中,我们对软件开发进行抽象.分割成各个模块或对象.例如,我们对API抽象成三个模块,Controller.Service.Comma ...

随机推荐

  1. 【C#】C#中的属性与字段

    目录结构: contents structure [+] 属性和字段的区别 无参属性 自动实现的属性 对象和集合初始化器 匿名类型 System.Tuple类型 有参属性 属性的可访问性 在这篇文章中 ...

  2. 【FTP】FTP服务器的搭建

    记录一下FTP服务器的搭建首先打开 程序和功能>打开或关闭Windows功能 进入到Windows功能界面:勾选FTP服务器.然后再在IIS界面,新建一个网站.右键网站,选择“添加到FTP发布” ...

  3. [svc]tomcat配置文件详解-最简单的基于mvn的war包

    tomcat安全管理规范 java&tomcat配置参考(多看看这位大牛的博客,写的很好) Tomcat系列之Java技术详解 http://blog.51cto.com/freeloda/1 ...

  4. samba需求,请建一个目录,所有用户都可以修改其中的任意文件(新建文件的权限问题)

    我的配置如下: [TMP] comment = Public Stuff path = /DATA/samba/tmp public = yes writable = yes create mode ...

  5. [Big Data - ZooKeeper] ZooKeeper: A Distributed Coordination Service for Distributed Applications

    ZooKeeper ZooKeeper: A Distributed Coordination Service for Distributed Applications Design Goals Da ...

  6. 【iCore1S 双核心板_FPGA】例程六:状态机实验——状态机使用

    核心代码: module FSM( input CLK_12M, input FPGA_KEY, output FPGA_LEDR, output FPGA_LEDG, output FPGA_LED ...

  7. Spring Session Redis

    http://www.infoq.com/cn/articles/Next-Generation-Session-Management-with-Spring-Session

  8. c#.net基础

    值类型:值类型的实例一般在线程的栈上分配 引用类型:引用类型的实例在线程的托管堆上分配 引用类型变量的Equals比较的是二者的引用地址而不是内部的值,值类型变量的Equals方法比较的是二者的值. ...

  9. Tensorflow 使用slim框架下的分类模型进行分类

    Tensorflow的slim框架可以写出像keras一样简单的代码来实现网络结构(虽然现在keras也已经集成在tf.contrib中了),而且models/slim提供了类似之前说过的object ...

  10. 《objective-c基础教程》学习笔记(五)—— 继承方法

    在上一篇博文中,我们将原先的纯C语言代码,编写成了用Objective-C(后面直接缩写成OC)的写法.使得代码在易读性上有明显提升,结构也更清晰.同时,也对面向对象的概念有了进一步的介绍和加深. 但 ...