准备一个接口,里面有两个方法doSome(),doOuther()

写一个类,实现上面这个接口

不改变这个类里面的方法,实现增强功能(也就是增加业务或者日志),在写一个类,用另外一个类,去增强代码。

写完增强代码,需要写一类把增强类整合到一起 ,这个类实现了InvocationHandler,这里面就能帮我们实现代理的功能

写完以上的代码,下面这个是测试以上写的内容是否能够实现代理的功能
public class MyTest{
public static void main(String[] args){
//使用jdk的proxy创建代理对象
//创建目标类
SomeService target = new SomeServiceImpl();
//创建InvocationHandler对象
InvocationHandler handler = new MyIncationHandler(target);
//使用Proxy创建代理
SomeService pro = (SomeService) Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),handler);
//通过代理执行方法,会调用handler中的invoke()
pro.dosome();
System.out.println("------------------");
pro.doOuther();
}
}