检察机关门户网站建设工作自查报告,wordpress 编辑权限,深圳建站网站网站公司,html5手机网站目录儿一、代理模式的作用二、实现代理的方式三、动态代理的实现3.1 jdk动态代理3.2 cglib动态代理一、代理模式的作用
功能增强: 基于某个功能#xff0c;再增加一些功能。 #xff08;比如目标类只负责核心功能#xff0c;其他附属功能通过代理类完成。代理类的方法名与目…
目录儿一、代理模式的作用二、实现代理的方式三、动态代理的实现3.1 jdk动态代理3.2 cglib动态代理一、代理模式的作用
功能增强: 基于某个功能再增加一些功能。 比如目标类只负责核心功能其他附属功能通过代理类完成。代理类的方法名与目标类的方法相同内容不同在核心功能外加了一些额外逻辑控制访问: 防止直接访问目标。 代理类代理目标类 目标类是实现目标功能的类 二、实现代理的方式 动态代理 特点 在程序执行过程中自动使用jdk反射机制创建代理对象而不需要写.java源文件并且动态指定要代理的目标类。 优点缺点 静态代理 特点 代理类是自己手动实现的。自己创建一个类作为代理类要代理的目标类是确定的。 优点 实现简单容易理解在不改变原目标对象代码的情况下对目标的功能进行拓展 缺点 当目标类增加了代理类可能要成倍的增加代理类过多当接口的功能增加或修改会影响所有实现类修改工作量大不好管理维护 。
三、动态代理的实现
3.1 jdk动态代理
基于接口实现代理 使用java反射包中的接口和类实现动态代理要求 代理类和工具类实现同一个接口。 其中反射包是java.lang,reflect里面有三个类InvocationHandler、Method、Proxy ① 创建目标接口
public interface Star{String sing(String name);void dance();
}② 创建目标类实现目标接口
public class BigStar implements Star {private String name;public BigStar(String name) {this.name name;}public String sing(String name) {System.out.println(this.name 正在唱歌儿: name);return 谢谢儿!谢谢儿!;}public void dance() {System.out.println(this.name 正在跳舞儿);}
}③ 创建对应的处理器实现InvocationHandler接口重写invoke方法(里面写增强业务)
public class StarInvocationHandlerT implements java.lang.reflect.InvocationHandler {private final T target; // 存储目标对象public StarInvocationHandler(T target) {this.target target;}Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println(method.getName() 方法执行前增强);Object result method.invoke(target, args);System.out.println(method.getName() 方法执行后增强);return result;}
}④ 创建工厂类为了简化创建代理对象的过程(这里用了泛型约束可以不用)
public class ProxyHandlerFactory {/*** 创建 Star 接口的代理对象*/public static T extends Star Star createStarProxy(T target) {return (Star) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new StarInvocationHandler(target));}}⑤ 测试调用
public class Main {public static void main(String[] args) {BigStar bigstar new BigStar(迪丽热巴);Star starProxy ProxyHandlerFactory.createStarProxy(bigstar);String result starProxy.sing(老公老公我爱你);System.out.println(result);}
}结果
3.2 cglib动态代理
基于继承实现代理 cglib是一个第三方工具库能够创建代理对象。 通过继承的方式实现动态代理通过继承目标类创建它的子类在子类中重写父类的同名方法实现功能的修改。因为是继承重写的方式实现代理因此要求目标类不能是final修饰要重写的方法也不能是final修饰的。
① 导入cglib依赖 ② 创建目标类
public class BigStar {private String name;public BigStar() {}public BigStar(String name) {this.name name;}public String sing(String name) {System.out.println(this.name 正在唱歌儿: name);return 谢谢儿!谢谢儿!;}public void dance() {System.out.println(this.name 正在跳舞儿);}
}③ 创建拦截器
public class BigStarMethodInterceptor implements MethodInterceptor {Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {System.out.println(method.getName() 方法增强前);Object result methodProxy.invokeSuper(o, objects); // 调用目标类方法System.out.println(method.getName() 方法增强前);return result;}
}④ 创建代理对象测试
public class TestMain {Testpublic void test() {// 创建与目标对象对应的拦截器BigStarMethodInterceptor bigStarMethodInterceptor new BigStarMethodInterceptor();// 创建一个增强器对象Enhancer enhancer new Enhancer();// 设置目标类enhancer.setSuperclass(BigStar.class);// 设置拦截处理器enhancer.setCallback(bigStarMethodInterceptor);// 创建代理对象(根据参数选择对应的 Constructor)BigStar bigStarProxy (BigStar) enhancer.create(new Class[]{String.class}, new Object[]{胖迪});// 调用方法String s bigStarProxy.sing(猪之歌);System.out.println(s);}
}运行结果 cglib的代理效率高于jdk代理。 如果想看看生成的代理对象长什么养可以通过设置一个系统属性并指定路径CgLib会把生成的代理对象写出到该位置 // CgLib提供的代理类生成器System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, D:\\idea_workspace\\ruoyi\\wipinfo_bigscreen_manager\\admin\\src);参考资料 哔哩哔哩 黑马程序员磊哥 【黑马磊哥】Java动态代理深入剖析真正搞懂Java核心设计模式代理设计模式 https://www.bilibili.com/video/BV1ue411N7GX?share_sourcecopy_web哔哩哔哩 动力节点 Java-JDK动态代理AOP使用及实现原理分析 https://www.bilibili.com/video/BV1HZ4y1p7F1?share_sourcecopy_web哔哩哔哩 上云_云哥 jdk动态代理cglib动态代理代理设计模式aop切面编程 https://www.bilibili.com/video/BV1tY411Z799?p6share_sourcecopy_web
文章转载自: http://www.morning.tpnxr.cn.gov.cn.tpnxr.cn http://www.morning.btpzn.cn.gov.cn.btpzn.cn http://www.morning.ttdxn.cn.gov.cn.ttdxn.cn http://www.morning.ghcfx.cn.gov.cn.ghcfx.cn http://www.morning.zpxwg.cn.gov.cn.zpxwg.cn http://www.morning.hyjpl.cn.gov.cn.hyjpl.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.nsncq.cn.gov.cn.nsncq.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.fhhry.cn.gov.cn.fhhry.cn http://www.morning.mymz.cn.gov.cn.mymz.cn http://www.morning.znlhc.cn.gov.cn.znlhc.cn http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.fhyhr.cn.gov.cn.fhyhr.cn http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.rhmpk.cn.gov.cn.rhmpk.cn http://www.morning.qykxj.cn.gov.cn.qykxj.cn http://www.morning.wnbqy.cn.gov.cn.wnbqy.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.c7497.cn.gov.cn.c7497.cn http://www.morning.slqgl.cn.gov.cn.slqgl.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.rqkzh.cn.gov.cn.rqkzh.cn http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.tkyry.cn.gov.cn.tkyry.cn http://www.morning.ymwcs.cn.gov.cn.ymwcs.cn http://www.morning.sqdjn.cn.gov.cn.sqdjn.cn http://www.morning.qbwmz.cn.gov.cn.qbwmz.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn http://www.morning.wnzgm.cn.gov.cn.wnzgm.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.kggxj.cn.gov.cn.kggxj.cn http://www.morning.smxyw.cn.gov.cn.smxyw.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.lrwsk.cn.gov.cn.lrwsk.cn http://www.morning.bdqpl.cn.gov.cn.bdqpl.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn http://www.morning.nlysd.cn.gov.cn.nlysd.cn http://www.morning.thbnt.cn.gov.cn.thbnt.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.dgxrz.cn.gov.cn.dgxrz.cn http://www.morning.srtw.cn.gov.cn.srtw.cn http://www.morning.snnkt.cn.gov.cn.snnkt.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.frzdt.cn.gov.cn.frzdt.cn http://www.morning.fhyhr.cn.gov.cn.fhyhr.cn http://www.morning.wrtw.cn.gov.cn.wrtw.cn http://www.morning.bhwz.cn.gov.cn.bhwz.cn http://www.morning.rnzgf.cn.gov.cn.rnzgf.cn http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.rsdm.cn.gov.cn.rsdm.cn http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.fxwkl.cn.gov.cn.fxwkl.cn http://www.morning.ktfnj.cn.gov.cn.ktfnj.cn http://www.morning.wqjpl.cn.gov.cn.wqjpl.cn http://www.morning.hlkxb.cn.gov.cn.hlkxb.cn http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.qkdjq.cn.gov.cn.qkdjq.cn http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.gxklx.cn.gov.cn.gxklx.cn http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn http://www.morning.ymrq.cn.gov.cn.ymrq.cn http://www.morning.nmkfy.cn.gov.cn.nmkfy.cn http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.rdlxh.cn.gov.cn.rdlxh.cn http://www.morning.ldcsw.cn.gov.cn.ldcsw.cn