不出意外,你可能只知道两种类代理的方式。一种是JDK自带的,另外一种是CGLIB。
创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、成都网站建设、前郭网络推广、微信小程序定制开发、前郭网络营销、前郭企业策划、前郭品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供前郭建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
我们先定义出一个接口和相应的实现类,方便后续使用代理类在方法中添加输出信息。
「定义接口」
- public interface IUserApi {
- String queryUserInfo();
- }
「实现接口」
- public class UserApi implements IUserApi {
- public String queryUserInfo() {
- return "沉淀、分享、成长,让自己和他人都能有所收获!";
- }
- }
好!接下来我们就给这个类方法使用代理加入一行额外输出的信息。
- @Test
- public void test_reflect() throws Exception {
- Class
clazz = UserApi.class; - Method queryUserInfo = clazz.getMethod("queryUserInfo");
- Object invoke = queryUserInfo.invoke(clazz.newInstance());
- System.out.println(invoke);
- }
- public class JDKProxy {
- public static
T getProxy(Class clazz) throws Exception { - ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- return (T) Proxy.newProxyInstance(classLoader, new Class[]{clazz}, new InvocationHandler() {
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- System.out.println(method.getName() + " 你被代理了,By JDKProxy!");
- return "沉淀、分享、成长,让自己和他人都能有所收获!";
- }
- });
- }
- }
- @Test
- public void test_JDKProxy() throws Exception {
- IUserApi userApi = JDKProxy.getProxy(IUserApi.class);
- String invoke = userApi.queryUserInfo();
- logger.info("测试结果:{}", invoke);
- }
- /**
- * 测试结果:
- *
- * queryUserInfo 你被代理了,By JDKProxy!
- * 19:55:47.319 [main] INFO org.itstack.interview.test.ApiTest - 测试结果: 沉淀、分享、成长,让自己和他人都能有所收获!
- *
- * Process finished with exit code 0
- */
- public class CglibProxy implements MethodInterceptor {
- public Object newInstall(Object object) {
- return Enhancer.create(object.getClass(), this);
- }
- public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
- System.out.println("我被CglibProxy代理了");
- return methodProxy.invokeSuper(o, objects);
- }
- }
- @Test
- public void test_CglibProxy() throws Exception {
- CglibProxy cglibProxy = new CglibProxy();
- UserApi userApi = (UserApi) cglibProxy.newInstall(new UserApi());
- String invoke = userApi.queryUserInfo();
- logger.info("测试结果:{}", invoke);
- }
- /**
- * 测试结果:
- *
- * queryUserInfo 你被代理了,By CglibProxy!
- * 19:55:47.319 [main] INFO org.itstack.interview.test.ApiTest - 测试结果: 沉淀、分享、成长,让自己和他人都能有所收获!
- *
- * Process finished with exit code 0
- */
- public class ASMProxy extends ClassLoader {
- public static
T getProxy(Class clazz) throws Exception { - ClassReader classReader = new ClassReader(clazz.getName());
- ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS);
- classReader.accept(new ClassVisitor(ASM5, classWriter) {
- @Override
- public MethodVisitor visitMethod(int access, final String name, String descriptor, String signature, String[] exceptions) {
- // 方法过滤
- if (!"queryUserInfo".equals(name))
- return super.visitMethod(access, name, descriptor, signature, exceptions);
- final MethodVisitor methodVisitor = super.visitMethod(access, name, descriptor, signature, exceptions);
- return new AdviceAdapter(ASM5, methodVisitor, access, name, descriptor) {
- @Override
- protected void onMethodEnter() {
- // 执行指令;获取静态属性
- methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
- // 加载常量 load constant
- methodVisitor.visitLdcInsn(name + " 你被代理了,By ASM!");
- // 调用方法
- methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
- super.onMethodEnter();
- }
- };
- }
- }, ClassReader.EXPAND_FRAMES);
- byte[] bytes = classWriter.toByteArray();
- return (T) new ASMProxy().defineClass(clazz.getName(), bytes, 0, bytes.length).newInstance();
- }
- }
- @Test
- public void test_ASMProxy() throws Exception {
- IUserApi userApi = ASMProxy.getProxy(UserApi.class);
- String invoke = userApi.queryUserInfo();
- logger.info("测试结果:{}", invoke);
- }
- /**
- * 测试结果:
- *
- * queryUserInfo 你被代理了,By ASM!
- * 20:12:26.791 [main] INFO org.itstack.interview.test.ApiTest - 测试结果: 沉淀、分享、成长,让自己和他人都能有所收获!
- *
- * Process finished with exit code 0
- */
- public class ByteBuddyProxy {
- public static
T getProxy(Class clazz) throws Exception { - DynamicType.Unloaded> dynamicType = new ByteBuddy()
- .subclass(clazz)
- .method(ElementMatchers.
named("queryUserInfo")) - .intercept(MethodDelegation.to(InvocationHandler.class))
- .make();
- return (T) dynamicType.load(Thread.currentThread().getContextClassLoader()).getLoaded().newInstance();
- }
- }
- @RuntimeType
- public static Object intercept(@Origin Method method, @AllArguments Object[] args, @SuperCall Callable> callable) throws Exception {
- System.out.println(method.getName() + " 你被代理了,By Byte-Buddy!");
- return callable.call();
- }
- @Test
- public void test_ByteBuddyProxy() throws Exception {
- IUserApi userApi = ByteBuddyProxy.getProxy(UserApi.class);
- String invoke = userApi.queryUserInfo();
- logger.info("测试结果:{}", invoke);
- }
- /**
- * 测试结果:
- *
- * queryUserInfo 你被代理了,By Byte-Buddy!
- * 20:19:44.498 [main] INFO org.itstack.interview.test.ApiTest - 测试结果: 沉淀、分享、成长,让自己和他人都能有所收获!
- *
- * Process finished with exit code 0
- */
- public class JavassistProxy extends ClassLoader {
- public static
T getProxy(Class clazz) throws Exception { - ClassPool pool = ClassPool.getDefault();
- // 获取类
- CtClass ctClass = pool.get(clazz.getName());
- // 获取方法
- CtMethod ctMethod = ctClass.getDeclaredMethod("queryUserInfo");
- // 方法前加强
- ctMethod.insertBefore("{System.out.println(\"" + ctMethod.getName() + " 你被代理了,By Javassist\");}");
- byte[] bytes = ctClass.toBytecode();
- return (T) new JavassistProxy().defineClass(clazz.getName(), bytes, 0, bytes.length).newInstance();
- }
- }
- @Test
- public void test_JavassistProxy() throws Exception {
- IUserApi userApi = JavassistProxy.getProxy(UserApi.class)
- String invoke = userApi.queryUserInfo();
- logger.info("测试结果:{}", invoke);
- }
- /**
- * 测试结果:
- *
- * queryUserInfo 你被代理了,By Javassist
- * 20:23:39.139 [main] INFO org.itstack.interview.test.ApiTest - 测试结果: 沉淀、分享、成长,让自己和他人都能有所收获!
- *
- * Process finished with exit code 0
- */
网站名称:除了JDK、CGLIB,还有3种类代理方式
网页URL:http://www.mswzjz.cn/qtweb/news4/152204.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能