jdk动态代理和cblib代理学习

jdk动态代理 解释-InvocationHandler
http://hi.baidu.com/huahua035/blog/item/6e5bff135b8412c2c3fd78b5.html
jdk动态代理  例子
http://www.blogjava.net/DoubleJ/archive/2008/03/04/183796.html
cglib代理例子
http://carterslam.iteye.com/blog/541009
cglib代理原理解释及例子
http://hnicypb.iteye.com/blog/263841
cglib api文档
http://cglib.sourceforge.net/apidocs/net/sf/cglib/proxy/MethodProxy.html
asm框架了解
http://www.ibm.com/developerworks/cn/java/j-lo-asm30/index.html

jdk动态代理例子
package cun.InvocationHandler;

public class Hello implements IHello{
	public void sayHello() {
		System.out.println("Hello , sytcun!");
	}
}



package cun.InvocationHandler;

public interface IHello {
	public void sayHello();
}


public class InvoHelloMain implements InvocationHandler{
	
	private Object obj;
	
	public InvoHelloMain(Object obj) {
		this.obj = obj;
	}
	
	public static void main(String args[]) {
		IHello hello = new Hello();
        
		IHello ihello = (IHello) InvoHelloMain.newInstance(hello);
        
		ihello.sayHello();
       
	}

	@SuppressWarnings("finally")
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		
		Object result = null;
		
		try{
			System.out.println("before hello, hello execute");
			result = method.invoke(proxy, args);
		return result;
		}catch(Exception ex) {
			ex.printStackTrace();
		} finally {
			System.out.println("execute method" + method.getName());
			return result;
		}
	}
	public static Object newInstance(Object obj)
    {
        return java.lang.reflect.Proxy.newProxyInstance(obj.getClass().getClassLoader(),
                obj.getClass().getInterfaces(), new InvoHelloMain(obj));
    }
	
}

你可能感兴趣的:(java,jdk,.net,IBM,Blog)