IT民工窝棚qbq.blog.chinaunix.net
qbq
全部博文(708)
国产(1)
欧美(1)
SEO(1)
CSS3(5)
TestNG(4)
HTML5(2)
iBatis(3)
URLRewrite(1)
WebService(1)
WebServer(12)
PHP(8)
OGNL(1)
AS2(2)
Multimedia(0)
Flex AS3(29)
面试(9)
Commet(1)
Ivy(2)
Bat(8)
Maven(18)
CSS(7)
Ext(9)
Spring问题集(4)
Word(1)
JFreeChart(2)
Groovy on Grails(14)
Python(1)
Portlet(3)
amCharts(4)
CSharp.NET(3)
Tools(1)
S2Dao(8)
HSQL(9)
taglib(28)
Source Safe(3)
JSTL(6)
EL(2)
Seasar-SAStruts(3)
Prototype(0)
JQuery(3)
DWR(7)
AJAX(14)
Guice(13)
Digit(2)
Notebook(4)
Log4J(8)
Servlet(2)
JSP(4)
Eclipse(12)
VB.NET(3)
DotNet(3)
JavaScript(63)
Thinking In Soft(10)
Framework(11)
English(0)
Struts2(14)
Struts(38)
Hibernate(10)
Spring(30)
HTML(14)
Web(5)
MYSQL(9)
SQLSERVER(1)
ORACLE(2)
SQL(3)
数据库(0)
DATABASE(0)
Windows(8)
JAVA(67)
Software(1)
Hardware(3)
OpenSource(2)
Microsoft(0)
Excel(4)
DIY(5)
Linux(4)
分类: Java
2010-04-07 16:37:15
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;interface AnInterface { public void doSth();}interface AnotherInterface { public void anotherdo();}class AClass implements AnInterface, AnotherInterface { public void doSth() { System.out.println("inside AClass.doSth"); } public void anotherdo() { System.out.println("inside AClass.anotherdo"); }}class SimpleInvocationHandler implements InvocationHandler { private Object realObject; public SimpleInvocationHandler(Object realObject) { this.realObject = realObject; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = null; System.out.println("Before calling: " + method.getName()); result = method.invoke(realObject, args); System.out.println("After calling: " + method.getName()); System.out.println(proxy.getClass().getName()); return result; }}public class ProxyTest { public static void main(String[] args) { AnInterface realSubject = new AClass(); AnInterface anProxy = (AnInterface) Proxy.newProxyInstance(realSubject.getClass().getClassLoader(), new Class[] { AnInterface.class }, new SimpleInvocationHandler(realSubject)); anProxy.doSth(); System.out.println(Proxy.isProxyClass(anProxy.getClass())); System.out.println(anProxy instanceof AnInterface); AnotherInterface anotherProxy = (AnotherInterface) Proxy.newProxyInstance(realSubject.getClass().getClassLoader(), new Class[] { AnotherInterface.class }, new SimpleInvocationHandler(realSubject)); anotherProxy.anotherdo(); System.out.println(Proxy.isProxyClass(anotherProxy.getClass())); System.out.println(anotherProxy instanceof AnotherInterface); }}
上一篇:简单ORM实现
下一篇:Flex AS3与Java的Socket通信
登录 注册