spring - 使用 setTarget 和 ProxyFactory(Object target) 方法为同一对象创建不同的 Aop 代理
问题描述
这是代码
//IUserService is a interface
IUserService userService=new UserServiceImpl();
NameMatchMethodPointcut pointcut=new NameMatchMethodPointcut();
pointcut.addMethodName("save*");
LogAdvice logAdvice=new LogAdvice();
Advisor advisor=new DefaultPointcutAdvisor(pointcut,logAdvice);
//when set the target in constructor JdkDynamicAopProxy will be created
//ProxyFactory proxyFactory=new ProxyFactory(userService);
ProxyFactory proxyFactory=new ProxyFactory();
proxyFactory.addAdvisor(advisor);
//if use setTarget to set the target ObjenesisCglibAopProxy will be created
proxyFactory.setTarget(userService);
IUserService proxy=(IUserService) proxyFactory.getProxy();
proxy.queryUser();
proxy.saveUser();
setTarget
set target using or in之间的区别在于Constructor
,当 in Constructor
theresetInterfaces(ClassUtils.getAllInterfaces(target));
但 not in 时setTarget
,我不知道为什么,因为我认为因为IUserService
是一个接口,JdkDynamicAopProxy
所以无论我如何设置目标都应该创建?spring源码版本为5.0.0.BUILD-SNAPSHOT
解决方案
推荐阅读
- r - R Shiny SelectizeInput:从分组选择中传递值不起作用
- swiftui - SwiftUI:按名称分组数据并显示在展开列表中
- python - 有没有办法可以在不使用循环的情况下多次应用 torch.mode
- javascript - 从 Indoor Bike Data 特征解码蓝牙数据
- c - 为什么不能打开源输入文件“ARMCM3.h”:?使用 ST-link 和 STM32F103ZET6
- python - 从运行 python 启动但不是从 python 脚本的 macOS 终端导入 numpy
- java - 四舍五入到最接近的百分之一
- java - 什么数据结构可以用来存储具有多个可比较属性的对象
- javascript - JS 模块“YAML”不使用文档格式的引号进行字符串化
- java - Amazon Keyspace (Cassandra) 查询没有节点可用于执行查询