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. C# Excel导入数据

    表 表的创建脚本 CREATE TABLE [dbo].[TB_PROJECTS_New1]( , ) NOT NULL, ) NULL, ) NULL, , ) NULL, , ) NULL, , ...

  2. 吴裕雄 python 机器学习——逻辑回归

    import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from mpl_toolkits.mplot ...

  3. ansible自动化

    一,工具与环境介绍   1.1 ansible简介 批量管理服务器的工具 无需部署agent,通过ssh进行管理 流行的自动化运维工具:https://github.com/ansible/ansib ...

  4. C#分割字符串并统计重复出现的次数

    static void Main(string[] args) { string ss = "12345678904682qwertyuioplkjhgfdsazxcvbnmmlpokuhy ...

  5. MongoDB集群的搭建

    一.环境准备 1.Centos7 2.mongodb3.4.10 3.三台机器IP分别是:192.168.1.100.192.168.1.135.192.168.1.136 二.mongdb数据库的安 ...

  6. nginx多域名、多证书

    环境: 一台nginx服务器 192.168.10.251 两台windowsserver2012 IIS服务器 (192.168.10.252.192.168.10.253) 从阿里云上下载ssl证 ...

  7. python入门day02数据类型

    字符串:数据类型的学习 #======================================基本使用====================================== #1.用途 ...

  8. 谈谈逆向android里面的so

    1. 加密sgf算法分析 2.gnugo瘦身记

  9. 使用nginx反向代理实现隐藏端口号

    使用nginx反向代理实现隐藏端口号 在服务器上下载安装nginx,主要是修改配置nginx.conf. 用proxy_pass里面配置要转发的域名+端口,相当于这一部分是被域名替换的部分,在http ...

  10. 如何判断Linux是32位还是64位

    getconf LONG_BIT