Java 動態代理樣本

來源:互聯網
上載者:User

public interface SomeClass {

    public abstract void someMethod();

    public abstract void someOtherMethod(final String text);
}

 

public class SomeClassImpl implements SomeClass{

    private String userName;

    public SomeClassImpl(final String userName) {
        this.userName = userName;
    }

    public void someMethod( ) {
        System.out.println(this.userName);
    }

    public void someOtherMethod(final String text) {
        System.out.println(text);
    }
}

 

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MethodCountingHandler implements InvocationHandler {

    /* whatever object, you can pass one in */
    private final Object impl;

    private int invocationCount = 0;

    /* constructor */
    public MethodCountingHandler(final Object impl) {
        this.impl = impl;
    }

    /* export the invocation Count */
    public int getInvocationCount( ) {
        return invocationCount;
    }

    /* implements the interface function of InvocationHandler */
    public Object invoke(Object proxy, Method meth, Object[] args)  throws Throwable {
        try {
            this.invocationCount++;
            Object result = meth.invoke(impl, args);
            return result;
        } catch (final InvocationTargetException ex) {
            throw ex.getTargetException( );
        }
    }
}

 

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class SomeClassFactory {

    public static final SomeClass getDynamicSomeClassProxy( ) {

        /* get a implement instance of SomeClass */
        SomeClassImpl impl = new SomeClassImpl(System.getProperty("user.name"));

        if ( !(impl instanceof SomeClass) )
            return null;

        /* construct a invocation handler with the impl instance */
        InvocationHandler handler = new MethodCountingHandler(impl);

        /* get the class info, and the class loader used by this factory */
        Class[] interfaces = new Class[] { SomeClass.class };
        ClassLoader loader = SomeClassFactory.class.getClassLoader( );

        /*
        * install the handler for all implementations of this interface in this class loader
        * and return the proxy instance which accords to SomeClass interface.
        */
        SomeClass proxy = (SomeClass)Proxy.newProxyInstance(loader,
                interfaces,
                handler);

        return proxy;
    }

}

 

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class DemoDynamicProxy {

    public static final void main(final String[] args) {

        SomeClass proxy = SomeClassFactory.getDynamicSomeClassProxy( );

        proxy.someMethod( );

        proxy.someOtherMethod("Our Proxy works!");

        /* get the handler associated with this proxy instance */
        InvocationHandler handler = Proxy.getInvocationHandler(proxy);

        if (handler instanceof MethodCountingHandler) {
            System.out.println(((MethodCountingHandler)handler).getInvocationCount( ));
        }
     }

}

Java的動態代理只支援基於Interface的Method Interception. 本例是從”Hardcore Java”一書中摘取出來的.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.