Proxy动态代理

 package com.test.dynamicproxy;

 public interface Subject {

     public void request();
}
 package com.test.dynamicproxy;

 public class RealSubject implements Subject {

     @Override
public void request() {
System.out.println("request() from RealSubject"); } }
 package com.test.dynamicproxy;

 import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; public class SubjectInvocationHandler implements InvocationHandler { private Object object; public SubjectInvocationHandler(Object obj) {
this.object = obj;
} @Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
method.invoke(object,args);
return null;
} }
 package com.test.dynamicproxy;

 import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy; public class Client { public static void main(String[] args) { RealSubject real = new RealSubject();
InvocationHandler ih = new SubjectInvocationHandler(real);
Class<?> clazz = real.getClass();
Subject s = (Subject)Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),ih); s.request(); //request() from RealSubject System.out.println(s.getClass().getName()); //$Proxy0
System.out.println(s instanceof Proxy); //true for(Class interf : s.getClass().getInterfaces()){
System.out.println(interf.getName()); //com.test.dynamicproxy.Subject
} // System.out.println(s.getClass().getPackage()); //null
// System.out.println(s.getClass().getSuperclass()); //class java.lang.reflect.Proxy
// System.out.println(Proxy.class.getPackage()); //package java.lang.reflect, Java Platform API Specification, version 1.6
} }
Object java.lang.reflect.Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException

Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler. This method is equivalent to:

     Proxy.getProxyClass(loader, interfaces).
getConstructor(new Class[] { InvocationHandler.class }).
newInstance(new Object[] { handler });

Proxy.newProxyInstance throws IllegalArgumentException for the same reasons that Proxy.getProxyClass does.

Parameters:
loader the class loader to define the proxy class
interfaces the list of interfaces for the proxy class to implement
h the invocation handler to dispatch method invocations to
Returns:
a proxy instance with the specified invocation handler of a proxy class that is defined by the specified class loader and that implements the specified interfaces
Throws:
IllegalArgumentException - if any of the restrictions on the parameters that may be passed to getProxyClass are violated
NullPointerException - if the interfaces array argument or any of its elements are null, or

Client 中   s   extends Proxy implements Subject

s.request() 方法调用流程 :

s 是 Subject 的 implementation  .s.request() 的实现

s 伪代码:

s extends Proxy implements Subject{

  InvocationHandler ih;

  ......

  public void request(){

    ih.invoke(....);

  }

}

Proxy动态代理的更多相关文章

  1. Proxy 动态代理 InvocationHandler CGLIB MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. 动态代理案例1:运用Proxy动态代理来增强方法

    动态代理案例1: /*要求:运用Proxy动态代理来增强方法 题目:    1.定义接口Fruit,其中有addFruit方法    2.定义实现类FruitImpl,实现Fruit接口    3.定 ...

  3. JAVA的Proxy动态代理在自动化测试中的应用

    JAVA的动态代理,在MYBATIS中应用的很广,其核心就是写一个interface,但不写实现类,然后用动态代理来实例化并执行这个interface中的方法,话不多说,来看一个实现的例子: 1.先定 ...

  4. 利用JDK的中Proxy动态代理实现Spring的AOP技术

    首先给出设计模式静态代理与动态代理的学习: http://layznet.iteye.com/blog/1182924   讲的不错 然后我们实现AOP 就要求我们对委托的所有方法的调用实现拦截 代理 ...

  5. 【Java深入研究】5、Proxy动态代理机制详解

    在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...

  6. Dynamic Proxy (动态代理模式)

    动态代理(运行期行为)主要有一个 Proxy类 和一个 InvocationHandler接口 动态代理角色: 1. 抽象主题角色 2. 真实主题角色(实现了抽象主题接口) 3. 动态代理主题角色(实 ...

  7. Proxy动态代理-增强方法

    增强对象的功能 设计模式:一些通用的解决固定问题的方式 装饰器模式 代理模式 概念: 在代理模式(Proxy Pattern)中,一个类代表另一个类的功能.这种类型的设计模式属于结构型模式. 在代理模 ...

  8. Java进阶 | Proxy动态代理机制详解

    一.Jvm加载对象 在说Java动态代理之前,还是要说一下Jvm加载对象的过程,这个依旧是理解动态代理的基础性原理: Java类即源代码程序.java类型文件,经过编译器编译之后就被转换成字节代码.c ...

  9. Java设计模式—Proxy动态代理模式

    代理:设计模式 代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为委托类预处理消息,过滤消息并转发消息,以及进行消息被委托类执行后的后续处理. 图 1. 代 ...

随机推荐

  1. UTI iPhone支持依文件后缀名打开应用

    本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6658492 iphoneapplicationprocessingfileurl ...

  2. 【BZOJ2707】[SDOI2012]走迷宫 Tarjan+拓扑排序+高斯消元+期望

    [BZOJ2707][SDOI2012]走迷宫 Description Morenan被困在了一个迷宫里.迷宫可以视为N个点M条边的有向图,其中Morenan处于起点S,迷宫的终点设为T.可惜的是,M ...

  3. 《从零开始学Swift》学习笔记(Day 18)——有几个分支语句?

    原创文章,欢迎转载.转载请注明:关东升的博客       分支语句又称条件语句,Swift编程语言提供了if.switch和guard三种分支语句. if语句 由if语句引导的选择结构有if结构.if ...

  4. JavaScript处理数据完成左侧二级菜单的搭建

    我们在项目中应用的后台管理框架基本上都是大同小异,左侧是一个二级菜单,点击选中的菜单,右侧对应的页面展示.我把前端页面封装数据的过程整理了一下,虽然不一定适合所有的管理页面,仅作为案例来参考,只是希望 ...

  5. 【python】-- Django ModelForm

    Django ModelForm Django的ModelForm的验证方式相比较form + Model的验证方式有下列区别: ModelForm没有form + Model的低耦合性 ModelF ...

  6. delphi下excel的操作

    1.首先引用comobj.varints单元 2.声明xlApp,xlBook, xlSheet,picture: Variant; 3.基本操作 xlApp:=CreateOleObject('Ex ...

  7. 【转】jstack简单使用

    转载记录下, 转载自https://www.cnblogs.com/chenpi/p/5377445.html 当我们运行java程序时,发现程序不动,但又不知道是哪里出问题时,可以使用JDK自带的j ...

  8. $(document).ready() $(window).load 及js的window.onload

    1.$(document).ready()  简写为$(function(){}) DOM结构绘制完成执行,而无需等到图片或其他媒体下载完毕. 2.$(window).load  在有时候确实我们有需 ...

  9. Python3 面向对象(1)

    面向.概述 面向过程: 根据业务逻辑从上到下写垒代码面向过程的设计的核心是过程,过程即解决问题的步骤, 面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西 优点: 极大降低了程序的 ...

  10. Android系统移植与调试之------->如何修改开机动画的两种方式剖析

    首先,我们先来分析一下源码: frameworks/base/cmds/bootanimation/BootAnimation.cpp 首先看一下定义的常量: BootAnimation::ready ...