<dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency>
package com.eyugame.test.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * jdk自带动态代理 * @author JYC506 * */ public class JDKProxy implements InvocationHandler { private Object target; /** * 绑定委托对象并返回一个代理类 * * @param T * 要代理的类 * @return * @throws IllegalAccessException * @throws InstantiationException */ @SuppressWarnings("unchecked") public <superClazz> superClazz getInstance(Class<?> T,Class<?> superClazz) throws InstantiationException, IllegalAccessException { this.target = T.newInstance(); // 取得代理对象 return (superClazz) Proxy.newProxyInstance(T.getClassLoader(), T.getInterfaces(), this); // 要绑定接口(这是一个缺陷,cglib弥补了这一缺陷) } /** * 绑定委托对象并返回一个代理类 * * @param T * 要代理的类 * @return * @throws IllegalAccessException * @throws InstantiationException */ @SuppressWarnings("unchecked") public <superClazz> superClazz getInstance(Object target ,Class<?> superClazz) { this.target =target; Class<?> clazz=this.target.getClass(); // 取得代理对象 return (superClazz) Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(), this); // 要绑定接口(这是一个缺陷,cglib弥补了这一缺陷) } /** * */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = null; System.out.println("事物开始"); result = method.invoke(target, args); System.out.println("事物结束"); return result; } public static void main(String[] args) { JDKProxy proxy = new JDKProxy(); Idog dog1 = null; Idog dog2=new Dog(); try { dog1 = proxy.getInstance(Dog.class,Idog.class); dog2=proxy.getInstance(new Dog(),Idog.class); dog1.doSomething(); dog2.setId(23); System.out.println(dog2.getId()); } catch (Exception e) { e.printStackTrace(); } } } class Dog implements Idog { private int id; public void doSomething() { System.out.println("call doSomething()"); } @Override public void setId(int id) { this.id=id; } @Override public int getId() { return this.id; } } interface Idog { void doSomething(); void setId(int id); int getId(); }运行结果
package com.eyugame.test.proxy; import java.lang.reflect.Method; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; /** * cglib动态代理 * @author JYC506 * */ public class CglibProxy implements MethodInterceptor { /** * 创建代理对象 * * @param target * @return */ @SuppressWarnings("unchecked") public <T> T getInstance(Class<?> T) { Enhancer enhancer = new Enhancer(); /* 指定父类 */ enhancer.setSuperclass(T); // 回调方法 enhancer.setCallback(this); // 创建代理对象 return (T)enhancer.create(); } @Override // 回调方法 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("事物开始"); Object o = proxy.invokeSuper(obj, args); System.out.println("事物结束"); return o; } public static void main(String[] args) { CglibProxy proxy = new CglibProxy(); People people = proxy.getInstance(People.class); people.setId(23); System.out.println(people.getId()); } } class People { private int id; private String username; private int age; public int getId() { System.out.println("获取id"); return id; } public void setId(int id) { System.out.println("给id赋值"); this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
原文地址:http://blog.csdn.net/h348592532/article/details/45971237