InvocationHandler接口实现动态代理
1、接口类(抽象角色)
public interface IHello {
public void hello(String name);
}
2、接口实现(真实角色)public void hello(String name);
}
public class HelloImpl implements IHello {
@Override
public void hello(String name) {
System.out.println("Hello, " + name);
}
}
@Override
public void hello(String name) {
System.out.println("Hello, " + name);
}
}
3、代理类实现(代理角色)
Code
4、使用代理
public class Demo {
public static void main(String[] args) {
HelloProxyImpl proxy = new HelloProxyImpl();
IHello helloProxy = (IHello) proxy.bind(new HelloImpl());
helloProxy.hello("Spring");
}
}
public static void main(String[] args) {
HelloProxyImpl proxy = new HelloProxyImpl();
IHello helloProxy = (IHello) proxy.bind(new HelloImpl());
helloProxy.hello("Spring");
}
}