以前一直以为jdk的动态代理不可以多层嵌套,今天做了一个实验发现是可以的。记录一下
类如下
public interface DemoService {
void saiHi(String name);
}
public class DemoServiceImpl implements DemoService {
@Override
public void saiHi(String name) {
System.out.println( "hi:" + name);
}
}
public class DemoServiceProxy implements InvocationHandler {
private static final AtomicInteger INTEGER=new AtomicInteger(1);
private Object target;
private Integer in;
public DemoServiceProxy(){
in=INTEGER.getAndIncrement();
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("执行前 in="+in);
Object object=method.invoke(target,args);
System.out.println("执行后 in="+in);
return object;
}
public Object newProxy(Object target){
this.target&