Java static proxy and dynamic proxy

本文深入解析Java中的代理机制,包括静态代理和动态代理的概念、实现方式及应用场景,通过代码实例详细说明如何使用这两种代理模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

----------------------------------------------------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------------------------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值