转:注解+动态代理例子

在公司没事干了,学习下动态代理模式(关于代理的基础只是有很多帖子都有我就不重复了),做了个注解动态代理的例子,给那些学习注解或动态代理的初学者。

花了将近1小时,没做个什么优化,大牛勿喷。

几个主要的类:

注解类:

Java代码   收藏代码
  1. package test2;  
  2.   
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7.   
  8.   
  9. @Retention(RetentionPolicy.RUNTIME)   
  10. @Target(ElementType.METHOD)  
  11. public @interface ProxyTag {  
  12.     public Class proxyClass();  
  13. }  
package test2;



import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;





@Retention(RetentionPolicy.RUNTIME) 

@Target(ElementType.METHOD)

public @interface ProxyTag {

	public Class proxyClass();

}

 

代理类父类:

Java代码   收藏代码
  1. import java.lang.reflect.InvocationHandler;  
  2. import java.lang.reflect.Method;  
  3. import java.lang.reflect.Proxy;  
  4.   
  5. public abstract class  ProxyBean implements InvocationHandler{  
  6.       
  7.     private Object o;  
  8.   
  9.       
  10.     private String methodName;  
  11.       
  12.     public Object bind(Object obj,String methodName){  
  13.         this.o = obj;  
  14.         return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);  
  15.     }  
  16.       
  17.       
  18.     public Object invoke(Object proxy, Method method, Object[] obj)  
  19.             throws Throwable {  
  20.         if(method.getName().equals(methodName)){  
  21.             this.before();  
  22.             Object result = method.invoke(o, obj);  
  23.             this.after();  
  24.             return result;  
  25.         }else{  
  26.             Object result = method.invoke(o, obj);  
  27.             return result;  
  28.         }  
  29.           
  30.     }  
  31.       
  32.     public abstract void before();  
  33.       
  34.     public abstract void after();  
  35. }  
import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

import java.lang.reflect.Proxy;



public abstract class  ProxyBean implements InvocationHandler{

	

	private Object o;



	

	private String methodName;

	

	public Object bind(Object obj,String methodName){

		this.o = obj;

		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);

	}

	

	

	public Object invoke(Object proxy, Method method, Object[] obj)

			throws Throwable {

		if(method.getName().equals(methodName)){

			this.before();

			Object result = method.invoke(o, obj);

			this.after();

			return result;

		}else{

			Object result = method.invoke(o, obj);

			return result;

		}

		

	}

	

	public abstract void before();

	

	public abstract void after();

}

代理工厂:

Java代码   收藏代码
  1. import java.lang.reflect.Constructor;  
  2. import java.lang.reflect.InvocationTargetException;  
  3. import java.lang.reflect.Method;  
  4.   
  5. public class ProxyFactory {  
  6.       
  7.       
  8.     public static <T> T getProxyBean(Class<T> clazz){  
  9.         T t = (T) newInstance(clazz);  
  10.         Method[] methods = clazz.getMethods();  
  11.           
  12.         for(int i=0;i<methods.length;i++){  
  13.             ProxyTag pt = methods[i].getAnnotation(ProxyTag.class);  
  14.             if(pt == null){  
  15.                 continue;  
  16.             }  
  17.             ProxyBean pb = (ProxyBean) newInstance(pt.proxyClass());  
  18.             t = (T) pb.bind(t, methods[i].getName());  
  19.         }  
  20.         return t;  
  21.     }  
  22.       
  23.     private static Object newInstance(final Class clazz){  
  24.         try {  
  25.             Constructor cons = clazz.getConstructor();  
  26.             return cons.newInstance(new Class[]{});  
  27.         } catch (SecurityException e) {  
  28.             // TODO Auto-generated catch block  
  29.             e.printStackTrace();  
  30.         } catch (NoSuchMethodException e) {  
  31.             // TODO Auto-generated catch block  
  32.             e.printStackTrace();  
  33.         } catch (IllegalArgumentException e) {  
  34.             // TODO Auto-generated catch block  
  35.             e.printStackTrace();  
  36.         } catch (InstantiationException e) {  
  37.             // TODO Auto-generated catch block  
  38.             e.printStackTrace();  
  39.         } catch (IllegalAccessException e) {  
  40.             // TODO Auto-generated catch block  
  41.             e.printStackTrace();  
  42.         } catch (InvocationTargetException e) {  
  43.             // TODO Auto-generated catch block  
  44.             e.printStackTrace();  
  45.         }  
  46.         return null;  
  47.     }  
  48. }  
import java.lang.reflect.Constructor;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;



public class ProxyFactory {

	

	

	public static <T> T getProxyBean(Class<T> clazz){

		T t = (T) newInstance(clazz);

		Method[] methods = clazz.getMethods();

		

		for(int i=0;i<methods.length;i++){

			ProxyTag pt = methods[i].getAnnotation(ProxyTag.class);

			if(pt == null){

				continue;

			}

			ProxyBean pb = (ProxyBean) newInstance(pt.proxyClass());

			t = (T) pb.bind(t, methods[i].getName());

		}

		return t;

	}

	

	private static Object newInstance(final Class clazz){

		try {

			Constructor cons = clazz.getConstructor();

			return cons.newInstance(new Class[]{});

		} catch (SecurityException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		} catch (NoSuchMethodException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		} catch (IllegalArgumentException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		} catch (InstantiationException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		} catch (IllegalAccessException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		} catch (InvocationTargetException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		}

		return null;

	}

}

 测试类:

Java代码   收藏代码
  1. package test2;  
  2.   
  3. public class ProxyClass extends ProxyBean{  
  4.   
  5.     public void after() {  
  6.         System.out.println("after....");  
  7.     }  
  8.   
  9.     public void before() {  
  10.         System.out.println("before....");  
  11.     }  
  12. }  
package test2;



public class ProxyClass extends ProxyBean{



	public void after() {

		System.out.println("after....");

	}



	public void before() {

		System.out.println("before....");

	}

}
Java代码   收藏代码
  1. public class ProxyClass2 extends ProxyBean{  
  2.   
  3.     public void after() {  
  4.         System.out.println("after2....");  
  5.     }  
  6.   
  7.     public void before() {  
  8.         System.out.println("before2....");  
  9.     }  
  10. }  
public class ProxyClass2 extends ProxyBean{



	public void after() {

		System.out.println("after2....");

	}



	public void before() {

		System.out.println("before2....");

	}

}
 
Java代码   收藏代码
  1. public interface IDemo {  
  2.     public void sayHello();  
  3.     public void sayHello2();  
  4. }  
public interface IDemo {

	public void sayHello();

	public void sayHello2();

}
 
Java代码   收藏代码
  1. public class Demo implements IDemo{  
  2.       
  3.     @ProxyTag(proxyClass=ProxyClass.class)  
  4.     public void sayHello(){  
  5.         System.out.println("hello....");  
  6.     }  
  7.       
  8.     @ProxyTag(proxyClass=ProxyClass2.class)  
  9.     public void sayHello2(){  
  10.         System.out.println("hello2....");  
  11.     }  
  12. }  
public class Demo implements IDemo{

	

	@ProxyTag(proxyClass=ProxyClass.class)

	public void sayHello(){

		System.out.println("hello....");

	}

	

	@ProxyTag(proxyClass=ProxyClass2.class)

	public void sayHello2(){

		System.out.println("hello2....");

	}

}

Java代码   收藏代码
  1. public class Test {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.           
  8.         IDemo demo = ProxyFactory.getProxyBean(Demo.class);  
  9.         demo.sayHello();  
  10.         System.out.println("-----------------------------");  
  11.         demo.sayHello2();  
  12.   
  13.     }  
  14.   
  15. }  
public class Test {



	/**

	 * @param args

	 */

	public static void main(String[] args) {

		

		IDemo demo = ProxyFactory.getProxyBean(Demo.class);

		demo.sayHello();

		System.out.println("-----------------------------");

		demo.sayHello2();



	}



}
Java代码   收藏代码
  1. before....  
  2. hello....  
  3. after....  
  4. -----------------------------  
  5. before2....  
  6. hello2....  
  7. after2.... 

你可能感兴趣的:(动态代理)