这是代码
//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
是一个接口,所以无论我如何设置目标,都应该创建JdkDynamicAopProxy
?
春天的源代码版本是5.0.0.BUILD-SNAPSHOT