跳转至

动态代理

动态代理

public class MyInvocationHandler implements InvocationHandler {

    private Object target  

    MyInvocationHandler(Object target){
        this.target=target  
    }


    public static void main(String[] args) {
        MyInvocationHandler invocationHandler=new MyInvocationHandler(new ArrayList<>())  
        Collection  collection= (Collection) Proxy.newProxyInstance(Collection.class.getClassLoader(),new Class[]{Collection.class},invocationHandler)  
        collection.add("abc")  
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("start...")  
        Object object= method.invoke(target, args)  
        System.out.println("end...")  
        return object  
    }
}

参考

评论