java动态代理上是否能再进行一层代理

CGLIB动态代理类

 

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor {  
    private Object target;  
  
    /** 
     * 创建代理对象 
     *  
     * @param target 
     * @return 
     */  
    public Object getInstance(Object target) {  
        this.target = target;  
        Enhancer enhancer = new Enhancer();  
        enhancer.setSuperclass(this.target.getClass());  
        // 回调方法  
        enhancer.setCallback(this);  
        // 创建代理对象  
        return enhancer.create();  
    }  
  
    @Override  
    // 回调方法  
    public Object intercept(Object obj, Method method, Object[] args,  
            MethodProxy proxy) throws Throwable {  
        System.out.println("事物开始CGLIB");  
        proxy.invokeSuper(obj, args);  
        System.out.println("事物结束CGLIB");  
        return null;  
    }  
    
  
}  

 

 java动态代理类

 

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.aop.framework.AopProxy;

public class ClassProxy implements InvocationHandler ,AopProxy{  
    private Object target;  
    /** 
     * 绑定委托对象并返回一个代理类 
     * @param target 
     * @return 
     */  
    public Object bind(Object target) {  
        this.target = target;  
        //取得代理对象  
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),  target.getClass().getInterfaces(), this);   //要绑定接口(这是一个缺陷,cglib弥补了这一缺陷)  
    }  
  
    @Override  
    /** 
     * 调用方法 
     */  
    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;  
    }

	@Override
	public Object getProxy() {
		System.out.println("test getProxy");
		return null;
	}

	@Override
	public Object getProxy(ClassLoader classLoader) {
		System.out.println("test getProxy");
		return null;
	}
}

 

尝试在java动态代理上进行java动态代理或者cglib代理,或者cglib代理上进行java动态代理或者cglib代理测试结果如下:

import java.lang.reflect.Modifier;

import org.springframework.aop.framework.AopProxy;

public class TestProxy {

	public static void main(String[] args) {
//		ClassProxy proxy = new ClassProxy();
//		AopProxy bookProxy = (AopProxy) proxy.bind(new ClassProxy());
//		bookProxy.getProxy();
//	    int i = bookProxy.getClass().getModifiers();
//        String mods = Modifier.toString(i);
//        System.out.println("Class modifiers: " + mods);
//		CglibProxy cglib = new CglibProxy();
//		ClassProxy bookCglib=(ClassProxy)cglib.getInstance(new ClassProxy());  
//		bookCglib.getProxy();
//		ClassProxy bookCglib2=(ClassProxy)cglib.getInstance(bookCglib);  
//		bookCglib2.getProxy();
		
		//JDK代理后再进行一层jdk代理,测试结果不可以,循环调用,造成代理的死循环
//		ClassProxy proxy = new ClassProxy();
//		AopProxy bookProxy = (AopProxy) proxy.bind(new ClassProxy());
//		AopProxy bookProxy2 = (AopProxy) proxy.bind(bookProxy);
//		bookProxy2.getProxy();
		
		
		//jdk代理后再用cglib进行代理,测试结果不行,因为jdk代理后的类是public final类型的,cglib不能对final的类进行代理
//		ClassProxy proxy = new ClassProxy();
//		AopProxy bookProxy = (AopProxy) proxy.bind(new ClassProxy());
//		bookProxy.getProxy();
//	    int i = bookProxy.getClass().getModifiers();
//        String mods = Modifier.toString(i);
//        System.out.println("Class modifiers: " + mods);
//		CglibProxy cglib = new CglibProxy();
//		ClassProxy bookCglib=(ClassProxy)cglib.getInstance(bookProxy);  
//		bookCglib.getProxy();
		
		//CGlib代理后再进行cglib代理,测试结果不可以  Caused by: java.lang.ClassFormatError: Duplicate method name&signature 
//		CglibProxy cglib = new CglibProxy();
//		ClassProxy bookCglib=(ClassProxy)cglib.getInstance(new ClassProxy());  
//		ClassProxy bookCglib2=(ClassProxy)cglib.getInstance(bookCglib);  
//		bookCglib2.getProxy();
		
		//CGlib代理后再进行jdk代理,测试结果不可以,因为CGlib代理后已经改变了类的签名,而jdk必须知道类的接口和实现
		CglibProxy cglib = new CglibProxy();
		ClassProxy bookCglib=(ClassProxy)cglib.getInstance(new ClassProxy());  
		bookCglib.getProxy();
		ClassProxy proxy = new ClassProxy();
		AopProxy bookProxy = (AopProxy) proxy.bind(bookCglib);
		bookProxy.getProxy();
		
	}

}
 

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