Spring 使用setTarget和ProxyFactory(对象目标)方法时为同一对象创建的不同Aop代理

Spring 使用setTarget和ProxyFactory(对象目标)方法时为同一对象创建的不同Aop代理,spring,spring-aop,Spring,Spring Aop,这是密码 //IUserService is a interface IUserService userService=new UserServiceImpl(); NameMatchMethodPointcut pointcut=new NameMatchMethodPointcut(); pointcut.addMethodName("save*"); LogAdvice logAdvice=new LogAdvice(); Advisor advisor=new DefaultP

这是密码

//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
或在
Constructor
中设置目标的区别在于,在
Constructor
中存在
setInterfaces(ClassUtils.getAllInterfaces(target))
但不是在
setTarget
中,我不知道为什么,因为
IUserService
是一个接口,所以无论我如何设置目标,都应该创建
jdkdynamicaoproxy

spring源代码版本是
5.0.0。使用
setTarget
时,构建快照

,建议您手动配置所有内容(因此您还需要调用
setInterfaces
)。当使用构造函数时,这意味着。@M.Deinum获取它