zoukankan      html  css  js  c++  java
  • 动态代理代码测试(jdk和glib)

    jdk动态代理代码编写测试(只针对实现接口的方法)
    package
    com.fh.proxy; public interface UserService { public abstract void add(); }
    package com.fh.proxy;
    
    public class UserServiceImpl implements UserService {
    
        
    
        @Override
        public void add() {
           System.out.println("***************add******************");
        }
    
    }
    View Code
    package com.fh.proxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    public class MyInvocationHandler implements InvocationHandler {
        
        private Object target;
    
        public MyInvocationHandler(Object target) {
           this.target = target;
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("**********before*************");
            Object result = method.invoke(target, args);
            System.out.println("*********after***************");
            return result;
        }
        
        public Object getProxy() {
            return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), 
                    target.getClass().getInterfaces(), this);
        }
    
    }
    package com.fh.proxy;
    
    import org.junit.Test;
    
    public class ProxyTest {
    
         @Test
         public void tests(){
            UserService user  =  new UserServiceImpl();
            MyInvocationHandler handler = new MyInvocationHandler(user);
            UserService proxy =(UserService) handler.getProxy();
            proxy.add();
        }
    
    }
    View Code
    cglib代理-针对没有实现接口的类进行代理,构建目标类的子类进行处理
    package
    com.fh.proxy; import java.lang.reflect.Method; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; public class EnhancerDemo { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(EnhancerDemo.class); enhancer.setCallback(new MethodInterceptorImpl()); EnhancerDemo demo = (EnhancerDemo) enhancer.create(); demo.test(); System.out.println(demo); } public void test() { System.out.println("EnhancerDemo test()"); } private static class MethodInterceptorImpl implements MethodInterceptor{ @Override public Object intercept(Object arg0, Method method, Object[] arg2, MethodProxy proxy) throws Throwable { System.out.println("before*******"+method); Object result = proxy.invokeSuper(arg0, arg2); System.out.println("after********"+method); return result; } } }

    在spring中动态代理是根据有无实现接口,但是可以强制选择cglib

  • 相关阅读:
    iOS之地理位置及定位系统 -- 入门笔记(用Swift)
    网易新闻iOS版使用的18个开源组件
    自学 iOS – 三十天三十个 Swift 项目
    iOS之UI--富文本总结
    IOS开发--横向流水布局实现
    IOS开发--仿制网易新闻
    Runtime 方法替换 和 动态添加实例方法 结合使用
    写给IOS开发工程师的网页前端入门笔记
    Runtime(动态添加属性)
    const,static,extern简介(重要)
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/9428508.html
Copyright © 2011-2022 走看看