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获取它