使用setTarget和ProxyFactory(Object target)方法为同一对象创建的不同Aop代理

时间:2018-09-06 06:29:54

标签: 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 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();

使用setTargetConstructor设置目标之间的区别在于,当Constructor中有setInterfaces(ClassUtils.getAllInterfaces(target));而不是setTarget时,我不会我不知道为什么,因为我认为IUserService是一个接口,所以无论我如何设置目标,都应该创建JdkDynamicAopProxy? 春天的源代码版本是5.0.0.BUILD-SNAPSHOT

0 个答案:

没有答案