Java 动态代理示例

ExpandedBlockStart.gif ContractedBlock.gif public   interface  SomeClass  dot.gif {
InBlock.gif
InBlock.gif    
public abstract void someMethod();
InBlock.gif
InBlock.gif    
public abstract void someOtherMethod(final String text);
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  SomeClassImpl implements SomeClass dot.gif {
InBlock.gif
InBlock.gif    
private String userName;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public SomeClassImpl(final String userName) dot.gif{
InBlock.gif        
this.userName = userName;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void someMethod( ) dot.gif{
InBlock.gif        System.
out.println(this.userName);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void someOtherMethod(final String text) dot.gif{
InBlock.gif        System.
out.println(text);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gifimport java.lang.reflect.InvocationHandler;
None.gifimport java.lang.reflect.InvocationTargetException;
None.gifimport java.lang.reflect.Method;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  MethodCountingHandler implements InvocationHandler  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//* whatever object, you can pass one in */
InBlock.gif    
private final Object impl;
InBlock.gif
InBlock.gif    
private int invocationCount = 0;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//* constructor */
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public MethodCountingHandler(final Object impl) dot.gif{
InBlock.gif        
this.impl = impl;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//* export the invocation Count */
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public int getInvocationCount( ) dot.gif{
InBlock.gif        
return invocationCount;
ExpandedSubBlockEnd.gif    }

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

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gifimport java.lang.reflect.InvocationHandler;
None.gifimport java.lang.reflect.Proxy;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  SomeClassFactory  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static final SomeClass getDynamicSomeClassProxy( ) dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//* get a implement instance of SomeClass */
InBlock.gif        SomeClassImpl impl 
= new SomeClassImpl(System.getProperty("user.name"));
InBlock.gif
InBlock.gif        
if ( !(impl instanceof SomeClass) )
InBlock.gif            
return null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//* construct a invocation handler with the impl instance */
InBlock.gif        InvocationHandler handler 
= new MethodCountingHandler(impl);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//* get the class info, and the class loader used by this factory */
ExpandedSubBlockStart.gifContractedSubBlock.gif        Class[] interfaces 
= new Class[] dot.gif{ SomeClass.class };
InBlock.gif        ClassLoader loader 
= SomeClassFactory.class.getClassLoader( );
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif        * install the handler for all implementations of this interface in this class loader
InBlock.gif        * and return the proxy instance which accords to SomeClass interface.
ExpandedSubBlockEnd.gif        
*/

InBlock.gif        SomeClass proxy 
= (SomeClass)Proxy.newProxyInstance(loader,
InBlock.gif                interfaces,
InBlock.gif                handler);
InBlock.gif
InBlock.gif        
return proxy;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gifimport java.lang.reflect.InvocationHandler;
None.gifimport java.lang.reflect.Proxy;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  DemoDynamicProxy  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static final void main(final String[] args) dot.gif{
InBlock.gif
InBlock.gif        SomeClass proxy 
= SomeClassFactory.getDynamicSomeClassProxy( );
InBlock.gif
InBlock.gif        proxy.someMethod( );
InBlock.gif
InBlock.gif        proxy.someOtherMethod(
"Our Proxy works!");
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//* get the handler associated with this proxy instance */
InBlock.gif        InvocationHandler handler 
= Proxy.getInvocationHandler(proxy);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (handler instanceof MethodCountingHandler) dot.gif{
InBlock.gif            System.
out.println(((MethodCountingHandler)handler).getInvocationCount( ));
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif     }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

Java的动态代理只支持基于Interface的Method Interception. 本例是从”Hardcore Java”一书中摘取出来的.

转载于:https://www.cnblogs.com/findsun/archive/2004/07/28/27873.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值