InvocationHandler is the interface implemented by the invocation handler of a proxy instance.

Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.

package cn.zno.newstar.base.utils.text;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; public class ProxyDemo {
public static void main(String[] args) {
Target target = new TargetImpl();
TargetIH ih = new TargetIH();
Target proxy = ih.newProxyInstance(target);
proxy.doSomething();
proxy.doSomethingElse();
}
} interface Target {
void doSomething(); void doSomethingElse();
} class TargetImpl implements Target { @Override
public void doSomething() {
System.out.println("TargetImpl doSomething");
} @Override
public void doSomethingElse() {
System.out.println("TargetImpl doSomethingElse");
}
} class TargetIH implements InvocationHandler {
private Object target; @SuppressWarnings("unchecked")
public <T> T newProxyInstance(T target) {
this.target = target;
Class<?> clazz = target.getClass();
// a proxy instance 的 invocation handler 实现 InvocationHandler 接口
return (T) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
} @Override
public Object invoke(Object proxy, Method method, Object[] args) {
System.out.println("proxy start");
Object result = null;
/*
* 这里可以正则方法名,判断参数,判断注解;不同场景动态代理不同的事
*
* */
try {
result = method.invoke(target, args);
} catch (Exception e) {
// do something
}
System.out.println("proxy end");
return result;
}
}

结果:

proxy start
TargetImpl doSomething
proxy end
proxy start
TargetImpl doSomethingElse
proxy end

基于接口的 InvocationHandler 动态代理(换种写法)的更多相关文章

  1. Java 动态代理 两种实现方法

    AOP的拦截功能是由java中的动态代理来实现的.说白了,就是在目标类的基础上增加切面逻辑,生成增强的目标类(该切面逻辑或者在目标类函数执行之前,或者目标类函数执行之后,或者在目标类函数抛出异常时候执 ...

  2. java Proxy InvocationHandler 动态代理实现详解

    spring 两大思想,其一是IOC,其二就是AOP..而AOP的原理就是java 的动态代理机制.这里主要记录java 动态代理的实现及相关类的说明. java  动态代理机制依赖于Invocati ...

  3. cglib动态代理是通过继承父类的方式进行代理的 不是通过接口方式进行动态代理的 因此可以对普通的类进行代理

    cglib动态代理是通过继承父类的方式进行代理的 不是通过接口方式进行动态代理的

  4. 基于继承的 MethodInterceptor 动态代理(换种写法)

    net.sf.cglib.proxy.Enhancer Generates dynamic subclasses to enable method interception. This class s ...

  5. 基于jdk proxy的动态代理模式

    代理模式 是spring AOP机制的实现基础,有必要学习一下. 有两种,一种是目标类有接口的, 采用JDK动态代理,一种是目标类没接口的,采用CGLIB动态代理. 先看一组代码, package c ...

  6. 基于 CGLIB 库的动态代理机制

    之前的文章我们详细的介绍了 JDK 自身的 API 所提供的一种动态代理的实现,它的实现相对而言是简单的,但是却有一个非常致命性的缺陷,就是只能为接口中的方法完成代理,而委托类自己的方法或者父类中的方 ...

  7. 动态代理:JDK原生动态代理(Java Proxy)和CGLIB动态代理原理+附静态态代理

    本文只是对原文的梳理总结,以及自行理解.自己总结的比较简单,而且不深入,不如直接看原文.不过自己梳理一遍更有助于理解. 详细可参考原文:http://www.cnblogs.com/Carpenter ...

  8. Java代理:静态代理、JDK动态代理和CGLIB动态代理

    代理模式(英语:Proxy Pattern)是程序设计中的一种设计模式.所谓的代理者是指一个类别可以作为其它东西的接口.代理者可以作任何东西的接口:网络连接.存储器中的大对象.文件或其它昂贵或无法复制 ...

  9. Java代理设计模式(Proxy)的四种具体实现:静态代理和动态代理

    面试问题:Java里的代理设计模式(Proxy Design Pattern)一共有几种实现方式?这个题目很像孔乙己问"茴香豆的茴字有哪几种写法?" 所谓代理模式,是指客户端(Cl ...

随机推荐

  1. crontab,定时任务执行找不到库or shell可执行,crontab 定时任务下就不能执行,tensorflow,ImportError: libcuda.so.1: cannot open shared object file: No such file or directory

    在线上启动一个定时任务,但是起来查看,发现任务执行找不到库,报cuda错误: ImportError: libcuda.so.1: cannot open shared object file: No ...

  2. Form-encoded method must contain at least one @Field.

    https://blog.csdn.net/liunian823/article/details/80290855 记得之前遇到过这个问题,并且记录笔记了,这次再翻笔记,却没有找到...搜索 了下. ...

  3. webpack打包优化

    https://www.cnblogs.com/vvjiang/p/9327903.html

  4. HAProxy 的acl应用

    非常好的博文推荐:http://blog.51cto.com/1992tao/1875563 官方文档:https://cbonte.github.io/haproxy-dconv/1.9/confi ...

  5. Failed to install gems via Bundler

    问题:在执行git push heroku master时,程序报错. 解决办法: 1.bundle update 2.git add . 3.git commit -m "message& ...

  6. HBase数据压缩算法编码探索

    摘要: 本文主要介绍了hbase对数据压缩,编码的支持,以及云hbase在社区基础上对数据压缩率和访问速度上了进行的改进. 前言 你可曾遇到这种需求,只有几百qps的冷数据缓存,却因为存储水位要浪费几 ...

  7. docker面试题集

    Docker的应用场景 Web 应用的自动化打包和发布. 自动化测试和持续集成.发布. 在服务型环境中部署和调整数据库或其他的后台应用. 从头编译或者扩展现有的OpenShift或Cloud Foun ...

  8. PCIe link up bug 分析

    Xilinx两块开发版PCIe link up时间相差很大,Virtex-6开发版PCIe link up时间超过60ms,而Virtex-7 PCIe link up时间只有~25ms.   分析过 ...

  9. CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-8CDH5安装和集群配置

    Cloudera Manager Server和Agent都启动以后,就可以进行CDH5的安装配置了.      准备文件 从 http://archive.cloudera.com/cdh5/par ...

  10. [转]Cloudera Manager和CDH5.8离线安装

    https://blog.csdn.net/zzq900503/article/details/52982828 https://www.cnblogs.com/felixzh/p/9082344.h ...