@Test
public void test(){
ProxyFactory proxyFactory = new ProxyFactory();
// 可改变target的引用
HotSwappableTargetSource targetSource = new HotSwappableTargetSource(new DemoOne("黄晓明"));
proxyFactory.setTargetSource(targetSource);
proxyFactory.addAdvice((MethodBeforeAdvice)(method,arg,target)->{
System.out.println("before.....");
});
Hello proxy = (Hello) proxyFactory.getProxy();
proxy.say("lisi");
targetSource.swap(new DemoOne("张大大"));// 必须是DemoOne 不能是其他类,应为生成的proxy是DemoOne的子类(cglib代理)
proxy.say("zhangsan");
}
这里proxyFactory 直接setTarget 使用了Cglib动态代理。如果是构造方法如下:会使用jdk基于接口的动态代理。
public ProxyFactory(Object target) {
setTarget(target);
setInterfaces(ClassUtils.getAllInterfaces(target)); //设置到AdvisedSupport 配置中
}



