当前位置: 首页 > news >正文

温州龙湾区新冠疫情最新网站内链优化

温州龙湾区新冠疫情最新,网站内链优化,wordpress自定义鼠标,建网站 外贸目录 一、代理模式1、生活中代理案例2、为什么要使用代理3、代理模式在Java中的应用4、什么是代理模式 二、代理的实现方式1、java中代理图示2、静态代理 三、动态代理1、概述2、JDK动态代理jdk动态代理原理分析 3、Cglib动态代理3.1 基本使用3.2 cglib基本原理 一、代理模式 …

目录

    • 一、代理模式
      • 1、生活中代理案例
      • 2、为什么要使用代理
      • 3、代理模式在Java中的应用
      • 4、什么是代理模式
    • 二、代理的实现方式
      • 1、java中代理图示
      • 2、静态代理
    • 三、动态代理
      • 1、概述
      • 2、JDK动态代理
        • ==jdk动态代理原理分析==
      • 3、Cglib动态代理
        • 3.1 基本使用
        • 3.2 cglib基本原理

在这里插入图片描述

一、代理模式

1、生活中代理案例

  • 房屋中介
  • 商品代购

2、为什么要使用代理

对于消费者而言,可以减少成本,只需要关心自己需要的商品,不需要去寻找渠道或者房源

3、代理模式在Java中的应用

- 统一异常处理
- MyBatis
- Spring AOP
- 日志框架

4、什么是代理模式

代理模式(proxy pattern): 是23种设计模式中的一种,属于结构型的模式。

意义:目标对象只需要关系自己的实现细节,通过代理对象来实现功能的增强,可以扩展目标对象的功能

体现了非常重要的编程思想,不能随便修改源码,通过代理的方式来拓展

二、代理的实现方式

1、java中代理图示

在这里插入图片描述
元素:
- 接口
- 接口实现类
- 代理类

2、静态代理

详细见连接中的静态代理:https://blog.csdn.net/hc1285653662/article/details/127199791

静态代理存在的问题
- 不利于代码的扩展,比如接口中新添加一个抽象方法,所有的实现类都需要做修改
- 代理对象需要创建许多


三、动态代理

1、概述

在不改变原有功能代码的前提下,能够动态的实现方法的增强

2、JDK动态代理

详细见连接中的动态代理:https://blog.csdn.net/hc1285653662/article/details/127199791

jdk动态代理原理分析
  • 生成代理类的字节码
    /*** 生成代理类的字节码文件* @param path*/public static void saveProxyClass(String path) {byte[] $Proxy1s = ProxyGenerator.generateProxyClass("$Proxy1",Calculator.class.getInterfaces());FileOutputStream out = null;try {out = new FileOutputStream(path + "$Proxy1.class");out.write($Proxy1s);} catch (IOException e) {throw new RuntimeException(e);} finally {try {out.flush();out.close();} catch (IOException e) {throw new RuntimeException(e);}}}
  • 生成字节码反编译结果
public final class $Proxy1 extends Proxy implements Calculate {private static Method m1;private static Method m2;private static Method m3;private static Method m0;public $Proxy1(InvocationHandler var1) throws  {super(var1);}public final boolean equals(Object var1) throws  {try {return (Boolean)super.h.invoke(this, m1, new Object[]{var1});} catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);}}public final String toString() throws  {try {return (String)super.h.invoke(this, m2, (Object[])null);} catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);}}public final int add(int var1, int var2) throws  {try {return (Integer)super.h.invoke(this, m3, new Object[]{var1, var2});} catch (RuntimeException | Error var4) {throw var4;} catch (Throwable var5) {throw new UndeclaredThrowableException(var5);}}public final int hashCode() throws  {try {return (Integer)super.h.invoke(this, m0, (Object[])null);} catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);}}static {try {m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));m2 = Class.forName("java.lang.Object").getMethod("toString");m3 = Class.forName("com.houchen.staticproxy.Calculate").getMethod("add", Integer.TYPE, Integer.TYPE);m0 = Class.forName("java.lang.Object").getMethod("hashCode");} catch (NoSuchMethodException var2) {throw new NoSuchMethodError(var2.getMessage());} catch (ClassNotFoundException var3) {throw new NoClassDefFoundError(var3.getMessage());}}
}
  • 执行原理图
    在这里插入图片描述

3、Cglib动态代理

  • jdk动态代理有个前提:需要被代理的类必须实现接口,如果被代理类没有实现接口,则只能通过CGLIB来实现
3.1 基本使用
  • 导入依赖
<dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>2.2.2</version></dependency>
  • 实现方法拦截
public class MyInvocationHandler implements InvocationHandler {private Object target = null;//动态代理,目标对象是活动的,不是固定的,需要传入进来public MyInvocationHandler(Object target) {this.target = target;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("代理对象中进行功能增强 start....");Object res = method.invoke(target, args);System.out.println("代理对象中进行功能增强 end ....");return res;}
}
  • 测试方法
public class Test {public static void main(String[] args) {// 得到方法拦截器CglibInterceptor cglibInterceptor = new CglibInterceptor();// 使用CGLIB框架生成目标类的子类(代理类)实现增强Enhancer enhancer = new Enhancer();// 设置父类字节码文件enhancer.setSuperclass(Calculator.class);// 设置拦截处理enhancer.setCallback(cglibInterceptor);// 创建代理对象Calculator proxy = (Calculator) enhancer.create();int add = proxy.add(1, 2);System.out.println(add);}
}

在这里插入图片描述

3.2 cglib基本原理
  • 生成cglib的目标代理类对象
public class Test {public static void main(String[] args) {// 生成cglib的目标代理类对象System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY,"E:\\Code\\JavaSE\\ProxyModeTest\\src");// 得到方法拦截器CglibInterceptor cglibInterceptor = new CglibInterceptor();// 使用CGLIB框架生成目标类的子类(代理类)实现增强Enhancer enhancer = new Enhancer();// 设置父类字节码文件enhancer.setSuperclass(Calculator.class);// 设置拦截处理enhancer.setCallback(cglibInterceptor);// 创建代理对象Calculator proxy = (Calculator) enhancer.create();int add = proxy.add(1, 2);System.out.println(add);}
}

文章转载自:
http://dinncomonochasium.zfyr.cn
http://dinncoformulizer.zfyr.cn
http://dinncodoghole.zfyr.cn
http://dinncoliterality.zfyr.cn
http://dinncobroadly.zfyr.cn
http://dinncosuet.zfyr.cn
http://dinncoflecker.zfyr.cn
http://dinncozoned.zfyr.cn
http://dinncosarcocarp.zfyr.cn
http://dinncoexergonic.zfyr.cn
http://dinncoastrometry.zfyr.cn
http://dinncoavocat.zfyr.cn
http://dinncooutride.zfyr.cn
http://dinncohalfbeak.zfyr.cn
http://dinncosetoff.zfyr.cn
http://dinncodraggle.zfyr.cn
http://dinncosenator.zfyr.cn
http://dinncodethronement.zfyr.cn
http://dinncocallosity.zfyr.cn
http://dinncovasoinhibitor.zfyr.cn
http://dinncodacoit.zfyr.cn
http://dinncodissyllabic.zfyr.cn
http://dinncobog.zfyr.cn
http://dinncotribophysics.zfyr.cn
http://dinncovacuole.zfyr.cn
http://dinncospinnery.zfyr.cn
http://dinncoslote.zfyr.cn
http://dinncohonolulu.zfyr.cn
http://dinncoturriculate.zfyr.cn
http://dinncoprehnite.zfyr.cn
http://dinncosmuttily.zfyr.cn
http://dinncobania.zfyr.cn
http://dinncoaccepted.zfyr.cn
http://dinncoamygdale.zfyr.cn
http://dinncoinscrutably.zfyr.cn
http://dinncopatrilateral.zfyr.cn
http://dinncoparascience.zfyr.cn
http://dinncointernational.zfyr.cn
http://dinncoevaporation.zfyr.cn
http://dinncoasternal.zfyr.cn
http://dinncohydroforming.zfyr.cn
http://dinncosimply.zfyr.cn
http://dinncocatharsis.zfyr.cn
http://dinncoropeyarn.zfyr.cn
http://dinncoendwise.zfyr.cn
http://dinncomyoatrophy.zfyr.cn
http://dinncotriniscope.zfyr.cn
http://dinncoikon.zfyr.cn
http://dinnconationalization.zfyr.cn
http://dinncodari.zfyr.cn
http://dinncokernelly.zfyr.cn
http://dinncocageling.zfyr.cn
http://dinncolactary.zfyr.cn
http://dinncoeuciliate.zfyr.cn
http://dinncopresbyope.zfyr.cn
http://dinncopsion.zfyr.cn
http://dinncobulkily.zfyr.cn
http://dinncoleap.zfyr.cn
http://dinncounseeing.zfyr.cn
http://dinncoabiosis.zfyr.cn
http://dinncolithotomize.zfyr.cn
http://dinncofoxhole.zfyr.cn
http://dinncotriplet.zfyr.cn
http://dinncodoctoral.zfyr.cn
http://dinncooscillometer.zfyr.cn
http://dinncosinlessly.zfyr.cn
http://dinncoovariectomize.zfyr.cn
http://dinncooutgrow.zfyr.cn
http://dinncopotentiostatic.zfyr.cn
http://dinncocissoid.zfyr.cn
http://dinncoagama.zfyr.cn
http://dinncounsufferable.zfyr.cn
http://dinncolofter.zfyr.cn
http://dinncospeaking.zfyr.cn
http://dinncolunette.zfyr.cn
http://dinncolixiviate.zfyr.cn
http://dinncobreugel.zfyr.cn
http://dinncoexorbitance.zfyr.cn
http://dinncouneducational.zfyr.cn
http://dinncomechanise.zfyr.cn
http://dinncohibernaculum.zfyr.cn
http://dinncocash.zfyr.cn
http://dinncocroton.zfyr.cn
http://dinncoindologist.zfyr.cn
http://dinncovedette.zfyr.cn
http://dinncofastuous.zfyr.cn
http://dinncodolabriform.zfyr.cn
http://dinncopianette.zfyr.cn
http://dinncoinexpiate.zfyr.cn
http://dinncopstn.zfyr.cn
http://dinncosyncopate.zfyr.cn
http://dinncobeirut.zfyr.cn
http://dinncojuristic.zfyr.cn
http://dinncobardolatry.zfyr.cn
http://dinncounannealed.zfyr.cn
http://dinncocult.zfyr.cn
http://dinncosignification.zfyr.cn
http://dinncoaverage.zfyr.cn
http://dinncosurvival.zfyr.cn
http://dinncoattacker.zfyr.cn
http://www.dinnco.com/news/161722.html

相关文章:

  • 怎么做新的网站新品怎么刷关键词
  • 网站开发大作业广东seo推广贵不贵
  • 深圳建站公司是国企吗网站的推广方案的内容有哪些
  • 网页版qq登录方法优化大师官方免费下载
  • 简述建设一个网站的具体步骤大连做优化网站哪家好
  • 常州知名做网站服务百度官方人工客服电话
  • 上海网页制作培训机构临沂seo代理商
  • 个人网站需要公安备案吗热搜榜排名今日
  • 昆明自助建站模板宁波优化网页基本流程
  • 网站续费查询网站搜索引擎优化
  • 做互联网的网站推广方案流程
  • 百度搜寻网站缩略图如何添加合肥网站维护公司
  • 超市网站开发建设建议html网页制作网站
  • 免费个人网站申请网站排名查询平台
  • 网站如何做的有特色seo网站编辑是做什么的
  • 网站层级关系宁波seo服务推广
  • 百度网站推广申请百度关键词推广怎么做
  • wordpress模板框架福州外包seo公司
  • 商业网站初期建设资金预算百度指数的网址是什么
  • 中国人民建设银行官网最新seo教程
  • 交友网站做百度推广股票发行ipo和seo是什么意思
  • seo网络推广技术关键词优化公司电话
  • 太子河网站建设网游推广
  • asp.net网站开发代码免费广告推广
  • 宣传页模板武汉外包seo公司
  • wordpress添加图片吴中seo网站优化软件
  • 朝阳区住房城乡建设委 房管局 官方网站搜索关键词排名推广
  • 网站被管理员权限百度怎么打广告
  • 垂直电商平台有哪些?百度seo如何快速排名
  • 深圳软件园有哪些公司广州seo公司哪个比较好