定义接口
package com.abc.service;
public interface ISomeService {
public void first();
public void second();
}
实现类
package com.abc.service;
public class SomeServiceImpl implements ISomeService {
public void first() {
System.out.println("1");
}
public void second() {
System.out.println("2");
}
}
代理类测试
package com.abc.test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import com.abc.service.ISomeService;
import com.abc.service.SomeServiceImpl;
public class MyTest {
public static void main(String[] args){
ISomeService target =new SomeServiceImpl();
ISomeService service =( ISomeService )Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler(){
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = method.invoke(target, args);
return result;
}
});
service.first();
service.second();
}
}
回顾动态代理 jdk动态代理proxy