cglib应用

java提供的基于Proxy、InvocationHandler的动态代理由于只能创建接口的代理对象,在实际应用中不能很好的满足需要,针对这个问题,另外一个开源框架cglib提供了一种实现方式,可以针对类进行代理对象的创建。

cglib官方网站:http://sourceforge.net/projects/cglib/files/

另外cglib依赖于asm(http://asm.ow2.org/)。

cglib使用示例:

HelloWorld.java

Java代码 复制代码 收藏代码
  1. public class HelloWorld{
  2. public String sayHello(String name){
  3. System.out.println("执行sayHello()");
  4. return "hello,"+name;
  5. }
  6. }
public class HelloWorld{
	
	public String sayHello(String name){
		System.out.println("执行sayHello()");
		return "hello,"+name;
	}
}

拦截器MyMethodInterceptor.java

Java代码 复制代码 收藏代码
  1. import java.lang.reflect.Method;
  2. import net.sf.cglib.proxy.MethodInterceptor;
  3. import net.sf.cglib.proxy.MethodProxy;
  4. public class MyMethodInterceptor implements MethodInterceptor{
  5. /*
  6. * obj 代理对象实例
  7. * method 源对象的方法名
  8. * args 传递给方法的实际入参
  9. * proxyMethod 与源对象中的method相对应的代理对象中的方法
  10. */
  11. public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
  12. System.out.println("执行方法"+method+"前");
  13. //执行源对象的method方法
  14. Object returnValue = proxy.invokeSuper(obj, args);
  15. System.out.println("执行方法"+method+"后");
  16. return returnValue;
  17. }
  18. }
import java.lang.reflect.Method;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class MyMethodInterceptor implements MethodInterceptor{

/* 
* obj 代理对象实例 
* method 源对象的方法名 
* args 传递给方法的实际入参 
* proxyMethod 与源对象中的method相对应的代理对象中的方法 
*/ 
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		
		System.out.println("执行方法"+method+"前");
		
		//执行源对象的method方法
		Object returnValue = proxy.invokeSuper(obj, args);
		
		System.out.println("执行方法"+method+"后");
		
		return returnValue;
	}

}

代理工厂ProxyFactory.java

Java代码 复制代码 收藏代码
  1. import net.sf.cglib.proxy.Enhancer;
  2. public class ProxyFactory {
  3. /*
  4. * 获得代理对象
  5. */
  6. public static Object getProxyObj(String clazz) throws Exception{
  7. Class<?> superClass = Class.forName(clazz);
  8. Enhancer hancer = new Enhancer();
  9. //设置代理对象的父类
  10. hancer.setSuperclass(superClass);
  11. //设置回调对象,即调用代理对象里面的方法时,实际上执行的是回调对象(里的intercept方法)。
  12. hancer.setCallback(new MyMethodInterceptor());
  13. //创建代理对象
  14. return hancer.create();
  15. }
  16. }
import net.sf.cglib.proxy.Enhancer;


public class ProxyFactory {

	/*
	 * 获得代理对象
	 */
	public static Object getProxyObj(String clazz) throws Exception{
		
		Class<?> superClass = Class.forName(clazz);
		
		Enhancer hancer = new Enhancer();
		
		//设置代理对象的父类
		hancer.setSuperclass(superClass);
		
		//设置回调对象,即调用代理对象里面的方法时,实际上执行的是回调对象(里的intercept方法)。
		hancer.setCallback(new MyMethodInterceptor());
		
		//创建代理对象
		return hancer.create();
	}	
}

测试类:

Java代码 复制代码 收藏代码
  1. public class Test {
  2. public static void main(String[] args) {
  3. try {
  4. HelloWorld hello = (HelloWorld) ProxyFactory.getProxyObj(HelloWorld.class.getName());
  5. System.out.println(hello.sayHello("huangqiqing"));
  6. } catch (Exception e) {
  7. e.printStackTrace();
  8. }
  9. }
  10. }
public class Test {

	public static void main(String[] args) {

		try {
			HelloWorld hello = (HelloWorld) ProxyFactory.getProxyObj(HelloWorld.class.getName());
			System.out.println(hello.sayHello("huangqiqing"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

输出结果:

执行方法public java.lang.String HelloWorld.sayHello(java.lang.String)前
执行sayHello()
执行方法public java.lang.String HelloWorld.sayHello(java.lang.String)后
hello,huangqiqing
执行方法protected void java.lang.Object.finalize() throws java.lang.Throwable前
执行方法protected void java.lang.Object.finalize() throws java.lang.Throwable后

猜你喜欢

转载自fjg0427.iteye.com/blog/1704497