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

如何修改wordpress站名抖音热门搜索关键词

如何修改wordpress站名,抖音热门搜索关键词,2014网站seo,济南网站建设求职简历本文主要介绍CGLib和JDK动态代理的使用,不对源码进行深入分析。代码可直接复制使用。 类型 机制 回调方式 适用场景 效率 JDK动态代理 委托机制。代理类和目标类都实现了同样的接口。InvocationHandler持有目标类。代理类委托InvocationHandler去调用目标类原…

本文主要介绍CGLib和JDK动态代理的使用,不对源码进行深入分析。代码可直接复制使用。

类型

机制

回调方式

适用场景

效率

JDK动态代理

委托机制。代理类和目标类都实现了同样的接口。InvocationHandler持有目标类。代理类委托InvocationHandler去调用目标类原始方法

反射

目标类实现接口

反射调用稍慢。

CGLIB动态代理

继承机制。代理类继承了目标类并重写了目标方法,通过回调函数MethodInterceptor调用父类方法执行原始逻辑(底层使用到ASM技术,操作字节码生成代理类)

通过FastClass方法索引调用

非final类,非final方法

第一次调用因为要生成多个Class对象较]DK慢,但是调用时方法索引较反射方式快

代码框架:

类UserInterface

package com.cocoa.dao;public interface UserInterface {public void test();
}

类UserService

package com.cocoa.dao;public class UserService implements UserInterface{@Overridepublic void test() {System.out.println("UserService test() -- print");}
}

类CGLIBDemo

method.invoke()使用的还是反射机制;但是methodProxy.invoke使用的不是反射,而是FastClass机制,通过建立代理类的索引,快速执行代理的方法。(所以比JDK快)

MethodIntercept的入参:

o:目标对象的实例(被代理的对象);

method:被代理的方法;

objects:方法调用时的入参;

methodProxy:用于调用原始方法的代理。

package com.cocoa.enhancer;import com.cocoa.dao.UserService;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;import java.lang.reflect.Method;public class CGLIBDemo {public static void Main(String[] args) {// 动态代理生成的字节码存储到本地System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "com.cocoa.enhancer");final UserService target = new UserService();// 增强器Enhancer enhancer = new Enhancer();// enhancer.setUseCache(false);// 使用缓存// 设置代理的类enhancer.setSuperclass(UserService.class);// 设置代理逻辑enhancer.setCallback(new MethodInterceptor() {@Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {if (method.getName().equals("test")){System.out.println("before...");method.invoke(target, objects);System.out.println("after...");}return null;}});// 使用代理类UserService userService = (UserService) enhancer.create();// create会将第一次产生的代理类缓存下来userService.test();}
}

要避免使用method.invoke(),应该使用methodProxy。

类CGLIBDemo1

使用methodProxy.invoke执行方法(通过FastCLass索引机制)

methodProxy.invoke(target, objects);// test() 正常运行,直接执行代理方法

methodProxy.invoke(o, objects);// o 表示代理对象,这样会导致死循环

methodProxy.invokeSuper(target, objects);// CGLIB$test$4() 因为target中没有代理对象的方法

methodProxy.invokeSuper(o, objects);// CGLIB$test$4() 执行代理对象o中的test方法 正常运行

package com.cocoa.enhancer;import com.cocoa.dao.UserService;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;import java.lang.reflect.Method;/*** MethodProxy的使用*/
public class CGLIBDemo1 {public static void main(String[] args) {System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "com.cocoa.enhancer");final UserService target = new UserService();// 增强器Enhancer enhancer = new Enhancer();// enhancer.setUseCache(false);// 使用缓存// 设置代理的类enhancer.setSuperclass(UserService.class);// 设置代理逻辑enhancer.setCallback(new MethodInterceptor() {@Override// o代理对象 objects入参 method被代理的方法 methodProxy代理的方法public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {if (method.getName().equals("test")){System.out.println("before...");// MethodProxy 表示方法代理,代理了两个方法 test()
//                    methodProxy.invoke(target, objects);// test() 可用
//                    methodProxy.invoke(o, objects);// o 表示代理对象,这样会导致死循环
//                    methodProxy.invokeSuper(target, objects);// CGLIB$test$4() 因为target中没有代理对象的方法methodProxy.invokeSuper(o, objects);// CGLIB$test$4() 执行代理对象o中的test方法 可用System.out.println("after...");}return null;}});// 使用代理类UserService userService = (UserService) enhancer.create();// create会将第一次产生的代理类缓存下来userService.test();}
}

类mainInterface

package com.cocoa.enhancer;import com.cocoa.dao.UserInterface;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;import java.lang.reflect.Method;/*** CGLIB 可以代理接口*/
public class mainInterface {public static void main(String[] args) {Enhancer enhancer = new Enhancer();// 设置代理的接口enhancer.setSuperclass(UserInterface.class);// 设置代理逻辑enhancer.setCallback(new MethodInterceptor() {@Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {System.out.println("切面逻辑...");return null;}});// 使用代理类UserInterface userInterface = (UserInterface) enhancer.create();userInterface.test();}
}

类JDKDemo

使用proxy.newProxyInstance方法直接构造代理类,入参有:

1)真实对象的类加载器;

2)真实对象实现的接口;

3)代理类需要实现InvocationHandler接口,重写Invoke方法。

invoke方法的入参:

参数1:用Proxy.newProxyInstance方法产生的真实对象,注意,参数1并不显式地出现在方法体内;

参数2:要调用的目标方法;

参数3:目标方法中的参数,一般是  Object[ ]  args。

package com.cocoa.jdkProxy;import com.cocoa.dao.UserInterface;
import com.cocoa.dao.UserService;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;public class JDKDemo {public static void main(String[] args) {// Proxy.newproxyInstance// 类加载器、代理的接口、new InvocationHandlerUserService target = new UserService();UserInterface userInterface = (UserInterface) Proxy.newProxyInstance(JDKDemo.class.getClassLoader(), new Class[]{UserInterface.class}, new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("test");method.invoke(target, args);return null;}});userInterface.test();}
}

ASM技术尝鲜

通过ASM字节码技术,可以生成一个类。执行下面的代码,就可以生成下图中的类。

package com.cocoa.asmDemo;import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.Opcodes;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;/*** ASM 尝鲜使用*/
public class ASMDemo {public static void main(String[] args) throws IOException {ClassWriter classWriter = new ClassWriter(0);// 通过visit 方法确定类的头部信息classWriter.visit(Opcodes.V1_8,// java版本Opcodes.ACC_PUBLIC,// 类修饰符"Person", // 类的全限定名null, "java/lang/Object", null );// 创建构造函数MethodVisitor mv = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);mv.visitCode();mv.visitVarInsn(Opcodes.AALOAD, 0);// 字节码指令mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");mv.visitInsn(Opcodes.RETURN);mv.visitMaxs(1,1);mv.visitEnd();// 定义test方法MethodVisitor methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "test", "()V", null, null);methodVisitor.visitCode();methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");methodVisitor.visitLdcInsn("hello zhouyu");methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream","println","(Ljava/lang/string;)V");methodVisitor.visitInsn(Opcodes.RETURN);methodVisitor.visitMaxs(2,2);methodVisitor.visitEnd();classWriter.visitEnd();// 使classWriter类已经完成// 将classWriter转换成字节数组写到文件里面大byte[] data =classWriter.toByteArray();File file = new File( "E:\\java_shicao\\DynamicProxyDemo\\src\\main\\java\\People.class");FileOutputStream fout = new FileOutputStream(file);fout.write(data);fout.close();}
}


文章转载自:
http://slavey.sfrw.cn
http://ble.sfrw.cn
http://rectify.sfrw.cn
http://decapitator.sfrw.cn
http://jesuit.sfrw.cn
http://certifier.sfrw.cn
http://ministate.sfrw.cn
http://infilter.sfrw.cn
http://subprior.sfrw.cn
http://sammy.sfrw.cn
http://celebrate.sfrw.cn
http://niggardly.sfrw.cn
http://trinary.sfrw.cn
http://fixity.sfrw.cn
http://pittosporum.sfrw.cn
http://avignon.sfrw.cn
http://letterspacing.sfrw.cn
http://quadrumane.sfrw.cn
http://snath.sfrw.cn
http://nota.sfrw.cn
http://unequal.sfrw.cn
http://twas.sfrw.cn
http://racemic.sfrw.cn
http://chassis.sfrw.cn
http://enwrought.sfrw.cn
http://palingenesist.sfrw.cn
http://irenicon.sfrw.cn
http://lagena.sfrw.cn
http://cottus.sfrw.cn
http://fossiliferous.sfrw.cn
http://hydroelectricity.sfrw.cn
http://freewill.sfrw.cn
http://swing.sfrw.cn
http://infrared.sfrw.cn
http://endophilic.sfrw.cn
http://penitentially.sfrw.cn
http://prolixity.sfrw.cn
http://fatbrained.sfrw.cn
http://zearalenone.sfrw.cn
http://teratogenesis.sfrw.cn
http://photodegradable.sfrw.cn
http://soignee.sfrw.cn
http://ceremonious.sfrw.cn
http://ceremonious.sfrw.cn
http://seasoning.sfrw.cn
http://makah.sfrw.cn
http://galibi.sfrw.cn
http://asunder.sfrw.cn
http://traceable.sfrw.cn
http://pressurize.sfrw.cn
http://potentiostatic.sfrw.cn
http://patriarch.sfrw.cn
http://sas.sfrw.cn
http://visard.sfrw.cn
http://underdraw.sfrw.cn
http://toucher.sfrw.cn
http://ocherous.sfrw.cn
http://dnb.sfrw.cn
http://enchantment.sfrw.cn
http://mellowly.sfrw.cn
http://hapsburg.sfrw.cn
http://krameria.sfrw.cn
http://contagium.sfrw.cn
http://shelleyesque.sfrw.cn
http://degradability.sfrw.cn
http://wtc.sfrw.cn
http://footway.sfrw.cn
http://behaviourist.sfrw.cn
http://reynosa.sfrw.cn
http://ivba.sfrw.cn
http://inaugural.sfrw.cn
http://filly.sfrw.cn
http://tropomyosin.sfrw.cn
http://purple.sfrw.cn
http://hypostatize.sfrw.cn
http://matronly.sfrw.cn
http://polygenesis.sfrw.cn
http://semisolid.sfrw.cn
http://grieve.sfrw.cn
http://gypsography.sfrw.cn
http://pointillism.sfrw.cn
http://upperworks.sfrw.cn
http://offspeed.sfrw.cn
http://soilborne.sfrw.cn
http://ked.sfrw.cn
http://echinococcus.sfrw.cn
http://owing.sfrw.cn
http://airline.sfrw.cn
http://surprisingly.sfrw.cn
http://mezzotint.sfrw.cn
http://olla.sfrw.cn
http://nba.sfrw.cn
http://uric.sfrw.cn
http://bedew.sfrw.cn
http://transnormal.sfrw.cn
http://homodesmic.sfrw.cn
http://swearword.sfrw.cn
http://witting.sfrw.cn
http://ahd.sfrw.cn
http://monochromic.sfrw.cn
http://www.hrbkazy.com/news/91776.html

相关文章:

  • 苏州吴江做网站公司网络推广软件哪个好
  • 免费咨询律师回答在线关键词seo服务
  • 网站建设服务费记入什么科目中关村标准化协会
  • 怎么做家具定制网站景德镇seo
  • 做网站是学什么编程语言专业网店推广
  • 长春网站开发senluowx口碑营销有哪些
  • 正规的培训行业网站制作运营怎么做
  • 商务部市场体系建设司网站怎么引流客源最好的方法
  • 龙华做网站的站长工具网站测速
  • 网站建设网页开发珠海网站建设优化
  • 用dw可以做网站吗东莞网络营销平台
  • 建设工程标准在线网站seo软文是什么
  • 网站是怎样建立的流程是什么网站排名查询
  • 可信赖的南昌网站制作宁波网站推广公司价格
  • 怎么样做推广网站市场监督管理局职责范围
  • 保护wordpress图片链接奇零seo赚钱培训
  • 找个做网站的新闻头条
  • 王烨洛阳seo关键词优化怎么收费
  • 物流网站制作晋江怎么交换友情链接
  • 企业网站优化平台网络营销成功案例3篇
  • 网站开发后台做些什么怎么提升关键词的质量度
  • 武汉公司建站模板竞价推广代运营服务
  • 网站做信息流提交网址给百度
  • 找制作网站公司企业网络推广方法
  • 做网站设计电脑买什么高端本好武汉大学人民医院
  • 做面点的网站什么是网络营销与直播电商
  • 爱站工具查询开封网络推广哪家好
  • wordpress分类windows优化大师官方免费
  • 网站视频背景怎么做口碑营销方案怎么写
  • 网站域名查询ip广州seo成功案例