Java静态代理和 * 总结
作者:LZHL 时间:2022-01-02 18:49:21
静态代理
第一种实现(基于接口):
1》接口
public interface Hello {
void say(String msg);
}
2》目标类,至少实现一个接口
public class HelloImpl implements Hello {
public void say(String msg) {
System.out.println("Hi,"+msg);
}
}
3》代理类(与目标类实现相同接口,从而保证功能一致)
public class HelloProxy implements Hello{
private Hello hello;
public HelloProxy(Hello hello){
this.hello = hello;
}
public void say(String msg){
before();
hello.say(msg);
after();
}
private void before(){
System.out.println("Before");
}
private void after(){
System.out.println("After");
}
}
3》测试
/**
* @Author LZHL
* @Create 2017-02-19 10:26
* @Description
*/
public class Main {
public static void main(String[] args) throws Exception {
HelloImpl target = new HelloImpl();
HelloProxy proxy = new HelloProxy(target);
proxy.say("LZHL");
}
}
第二种实现(基于目标类):
1>目标类
public class HelloTarget {
public void sayHello(String name){
System.out.println("Hi,"+name);
}
}
2>代理类(通过继承目标类,保证功能一致)
public class HelloProxy extends HelloTarget{
private HelloTarget target;
public HelloProxy(HelloTarget target){
this.target = target;
}
@Override
public void sayHello(String name) {
this.before();
target.sayHello(name);
this.after();
}
private void before(){
System.out.println("Before");
}
private void after(){
System.out.println("After");
}
}
3>测试
public class Main {
public static void main(String[] args) throws Exception {
HelloTarget target = new HelloTarget();
HelloProxy proxy= new HelloProxy(target);
proxy.sayHello("LZHL");
}
}
*
* 的代理类是在程序运行期间动态生成的,也有两种实现,一种是JDK * ,一种是CGLib *
1》JDK * (基于接口实现,与目标类实现相同接口,从而保证功能一致)
/**
* @Author LZHL
* @Create 2017-02-19 12:46
* @Description
*/
public class Main {
public static void main(String[] args){
final HelloImpl target = new HelloImpl();
Object proxyInstance = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
/*
* proxy: 代理对象
* method: 目标对象的方法对象
* args: 目标对象方法的参数
* return: 目标对象方法的返回值
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before");
Object retValue = method.invoke(target, args);
System.out.println("after");
return retValue;
}
});
Hello proxy = (Hello) proxyInstance;
proxy.say("LYX");
//可以把InvocationHandler提取出来,单独写一个类,为了方便大家看,这里我用内部类的形式
class JDKProxy implements InvocationHandler {
private Object target;
public JDKProxy(Object target){
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
before();
Object result = method.invoke(target, args);
after();
return result;
}
private void before(){
System.out.println("Before");
}
private void after(){
System.out.println("After");
}
}
InvocationHandler ih = new JDKProxy(target);
Object proxyInstance2 = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), ih);
Hello proxy2 = (Hello) proxyInstance2;
proxy2.say("LZHL");
}
}
2》CGLib * (基于目标类,通过继承目标类,从而保证功能一致),需要导入cglib-3.2.4.jar包
pom.xml
<dependencies>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>
</dependencies>
1)目标类
public class Hi {
public void sayHi(String msg){
System.out.println("Hi,"+msg);
}
}
2)测试
/**
* @Author LZHL
* @Create 2017-02-19 13:19
* @Description
*/
public class Main {
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
//设置父类
enhancer.setSuperclass(Hi.class);
//设置回调函数
enhancer.setCallback(new MethodInterceptor() {
public Object intercept(Object target, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("before");
Object retValue = methodProxy.invokeSuper(target, args);
System.out.println("after");
return retValue;
}
});
Object proxy = enhancer.create();
Hi hi = (Hi) proxy;
hi.sayHi("LXY");
//可以把MethodInterceptor提取出来,单独写一个类,为了方便大家看,这里我用内部类的形式
class CGLibProxy implements MethodInterceptor {
public <T> T getProxy(Class<T> clazz){
return (T) Enhancer.create(clazz, this);
}
public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable {
before();
Object result = proxy.invokeSuper(target, args);
after();
return result;
}
private void before(){
System.out.println("Before");
}
private void after(){
System.out.println("After");
}
}
CGLibProxy cgLibProxy = new CGLibProxy();
Hi hi2 = cgLibProxy.getProxy(Hi.class);
hi2.sayHi("LZHL");
}
}
以上所述是小编给大家介绍的Java静态代理和 * 总结,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
来源:http://www.cnblogs.com/lzhl/p/6416063.html
标签:java,静态代理, ,
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java封装前端查询条件通用版
2023-06-24 12:06:15
java秒杀之redis限流操作详解
2022-07-08 09:26:57
java数据结构与算法之noDups去除重复项算法示例
2023-06-19 08:50:34
Mybatis如何配置连接池
2021-10-22 03:06:47
MyBatis找不到mapper文件的实现
2023-12-15 09:22:11
![](https://img.aspxhome.com/file/2023/1/61131_0s.png)
SpringBoot通过自定义注解实现参数校验
2023-09-21 21:11:02
![](https://img.aspxhome.com/file/2023/3/58343_0s.png)
SpringBoot异步调用方法并接收返回值
2023-08-16 22:33:49
![](https://img.aspxhome.com/file/2023/5/58055_0s.jpg)
Java实现经典游戏复杂迷宫
2021-05-29 22:33:34
![](https://img.aspxhome.com/file/2023/1/63301_0s.png)
java必学必会之线程(2)
2023-11-09 10:22:35
![](https://img.aspxhome.com/file/2023/2/58742_0s.png)
SpringBoot 内置工具类的使用
2021-08-26 11:42:21
Unity实现俄罗斯方块(一)
2021-06-07 15:30:24
Spring Security实现用户名密码登录详解
2021-05-24 14:32:36
![](https://img.aspxhome.com/file/2023/1/61051_0s.png)
springboot+vue制作后台管理系统项目
2022-02-26 06:08:37
![](https://img.aspxhome.com/file/2023/4/69654_0s.png)
浅谈spring中scope作用域
2022-08-18 10:42:53
Jenkins安装以及邮件配置详解
2023-04-20 12:42:39
![](https://img.aspxhome.com/file/2023/1/63701_0s.png)
Java开发之Lombok指南
2022-11-19 21:49:28
![](https://img.aspxhome.com/file/2023/2/64202_0s.jpg)
Java进阶:Struts多模块的技巧
2023-06-18 09:40:47
java实现鲜花销售系统
2023-08-29 20:23:42
![](https://img.aspxhome.com/file/2023/3/58333_0s.jpg)
Dwr3.0纯注解(纯Java Code配置)配置与应用浅析二之前端调用后端
2023-08-19 17:32:33
![](https://img.aspxhome.com/file/2023/3/57693_0s.png)
java分页工具类的使用方法
2023-08-17 02:00:14