cglib calling process

This article do not speak how cglib application, only to see calls from the outermost to innermost dynamic proxy class is the proxy class from the perspective of eclipse debugging, after which an intermediate step.

 

Ado, directly on the code

The proxy class:

public class MySubject {
    public void doSomeThing() {
        System.err.println("Do some thing!");
    }    
}

 

 

Interceptor:

public class CgLibMethodInterceptor implements MethodInterceptor {
	@Override
	public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		System.err.println("before ");
		Object result = proxy.invokeSuper(target, args);
		System.err.println("after " + result);
		return result;
	}

} 

Test cglib dynamic proxy:

public class CglibProxyWriter {
	public static void main(String[] args) {
		System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\cglib-class\\");
		MySubject subject= (MySubject) Enhancer.create(MySubject.class,new CgLibMethodInterceptor());		
		subject.doSomeThing();
	}
}

 

 

From the call stack you can clearly see the outermost layer to the innermost calling logic.

MySubject$$EnhancerByCGLIB$$24226c3c(MySubject).doSomeThing() line: 6
MySubject$$EnhancerByCGLIB$$24226c3c.CGLIB$doSomeThing$0() line: not available
MySubject$$EnhancerByCGLIB$$24226c3c$$FastClassByCGLIB$$a7941ac2.invoke(int, Object, Object[]) line: not available
MethodProxy.invokeSuper(Object, Object[]) line: 244
CgLibMethodInterceptor.intercept(Object, Method, Object[], MethodProxy) line: 14
MySubject$$EnhancerByCGLIB$$24226c3c.doSomeThing() line: not available
CglibProxyWriter.main(String[]) line: 16

 

MySubject$$EnhancerByCGLIB$$24226c3c(MySubject).doSomeThing() line: 6
 
 

 MySubject$$EnhancerByCGLIB$$24226c3c.CGLIB$doSomeThing$0() line: not available 

 

 

MySubject$$EnhancerByCGLIB$$24226c3c$$FastClassByCGLIB$$a7941ac2.invoke(int, Object, Object[]) line: not available

 

 
 

 MethodProxy.invokeSuper(Object, Object[]) line: 244

 CgLibMethodInterceptor.intercept(Object, Method, Object[], MethodProxy) line: 14

MySubject$$EnhancerByCGLIB$$24226c3c.doSomeThing() line: not available

 CglibProxyWriter.main(String[]) line: 16

 

 

 

 

  

eclipse can not add breakpoints when debugging, cglib dynamically generated proxy class needs decompiling tool to view, capture has provided the decompiled code.

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/pmh905001/p/11456150.html