----------------------------------------------------static proxy-----------------------------------------------------------------------------
Interface:
public interface HelloService {
public String echo(String msg);
public Date getTime();
}
Impl:
public class HelloServiceImpl implements HelloService {
public String echo(String msg) {
// TODO Auto-generated method stub
return "echo:" + msg;
}
public Date getTime() {
// TODO Auto-generated method stub
return new Date();
}
}
proxy:
public class HelloServoceProxy implements HelloService {
/**
* 表示被代理的HelloService实例
*/
private HelloService helloService;
public HelloServoceProxy(HelloService helloService) {
this.helloService = helloService;
}
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
public String echo(String msg) {
System.out.println("预处理");
String result = helloService.echo(msg);
System.out.println("事后处理");
return result;
}
public Date getTime() {
System.out.println(" 预处理");
Date date = helloService.getTime();
System.out.println("事后处理");
return date;
}
}
client:
public class Client {
public static void main(String args[]){
HelloService helloService=new HelloServiceImpl();
HelloService helloServiceProxy=new HelloServoceProxy(helloService);
System.out.println("代理后的处理结果:"+helloServiceProxy.echo("猴哥"));
}
}
---------------------------------------------------end------------------------------------------------------------
Dynamic proxy:
----------------------------------------------Begin--------------------------------------------------
ProxyClass:
public class HelloServiceProxyFactory {
public static HelloService getHelloServiceProxy(
final HelloService helloService) {
/**
* 创建一个实现了InvocationHandler接口的的内部匿名类
*/
InvocationHandler handler = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("预处理...");
Object result = method.invoke(helloService, args);
System.out.println("事后处理");
return result;
}
};
Class classtype = HelloService.class;
return (HelloService) Proxy.newProxyInstance(
classtype.getClassLoader(), new Class[] { classtype }, handler);
}
}
Client:
public class Client {
public static void main(String args[]) {
HelloService helloService = new HelloServiceImpl();
HelloService helloServiceProxy = HelloServiceProxyFactory
.getHelloServiceProxy(helloService);
System.out
.println("动态代理类名称为:" + helloServiceProxy.getClass().getName());
System.out.println("输出结果:" + helloServiceProxy.echo("猴哥哥"));
}
}
---------------------------------------------End------------------------------------------------------------
Interface:
public interface HelloService {
public String echo(String msg);
public Date getTime();
}
Impl:
public class HelloServiceImpl implements HelloService {
public String echo(String msg) {
// TODO Auto-generated method stub
return "echo:" + msg;
}
public Date getTime() {
// TODO Auto-generated method stub
return new Date();
}
}
proxy:
public class HelloServoceProxy implements HelloService {
/**
* 表示被代理的HelloService实例
*/
private HelloService helloService;
public HelloServoceProxy(HelloService helloService) {
this.helloService = helloService;
}
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
public String echo(String msg) {
System.out.println("预处理");
String result = helloService.echo(msg);
System.out.println("事后处理");
return result;
}
public Date getTime() {
System.out.println(" 预处理");
Date date = helloService.getTime();
System.out.println("事后处理");
return date;
}
}
client:
public class Client {
public static void main(String args[]){
HelloService helloService=new HelloServiceImpl();
HelloService helloServiceProxy=new HelloServoceProxy(helloService);
System.out.println("代理后的处理结果:"+helloServiceProxy.echo("猴哥"));
}
}
---------------------------------------------------end------------------------------------------------------------
Dynamic proxy:
----------------------------------------------Begin--------------------------------------------------
ProxyClass:
public class HelloServiceProxyFactory {
public static HelloService getHelloServiceProxy(
final HelloService helloService) {
/**
* 创建一个实现了InvocationHandler接口的的内部匿名类
*/
InvocationHandler handler = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("预处理...");
Object result = method.invoke(helloService, args);
System.out.println("事后处理");
return result;
}
};
Class classtype = HelloService.class;
return (HelloService) Proxy.newProxyInstance(
classtype.getClassLoader(), new Class[] { classtype }, handler);
}
}
Client:
public class Client {
public static void main(String args[]) {
HelloService helloService = new HelloServiceImpl();
HelloService helloServiceProxy = HelloServiceProxyFactory
.getHelloServiceProxy(helloService);
System.out
.println("动态代理类名称为:" + helloServiceProxy.getClass().getName());
System.out.println("输出结果:" + helloServiceProxy.echo("猴哥哥"));
}
}
---------------------------------------------End------------------------------------------------------------