cglib代理一个类时会产生3个class文件,
上面标红部分的类就是两个相关类对应的fastclass文件,HelloServiceImpl
F
a
s
t
C
l
a
s
s
B
y
C
G
L
I
B
FastClassByCGLIB
FastClassByCGLIBbc82cc02.class这个文件是被代理类对应的fastclass文件,HelloServiceImpl
E
n
h
a
n
c
e
r
B
y
C
G
L
I
B
EnhancerByCGLIB
EnhancerByCGLIB75d5f5ba
F
a
s
t
C
l
a
s
s
B
y
C
G
L
I
B
FastClassByCGLIB
FastClassByCGLIB15a4adc1.class是产生的代理类对应的fastclass文件,下面看我们经常使用的代码:
Enhancer enh = new Enhancer();
enh.setSuperclass(HelloServiceImpl.class);
HelloMethodInterceptor inteceptor = new HelloMethodInterceptor();
HelloServiceImpl hello = new HelloServiceImpl();
inteceptor.setTarget(hello);
enh.setCallbacks(new Callback[]{inteceptor});
HelloServiceImpl serv = (HelloServiceImpl)enh.create();
serv.sayHello();
上面标红部分代码产生的实例就是HelloServiceImpl
E
n
h
a
n
c
e
r
B
y
C
G
L
I
B
EnhancerByCGLIB
EnhancerByCGLIB75d5f5ba.class这个类对象的实例,下面看下这个类相关部分代码:
public final void sayHello() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
var10000.intercept(this, CGLIB$sayHello
0
0
0Method, CGLIB
e
m
p
t
y
A
r
g
s
,
C
G
L
I
B
emptyArgs, CGLIB
emptyArgs,CGLIBsayHello
0
0
0Proxy);
} else {
super.sayHello();
}
}
上面的CALLBACK_0对应的就是HelloMethodInterceptor这个类,所以serv.sayHello()会转而调用HelloMethodInterceptor的intercept方法,下面看下HelloMethodInterceptor这个类:
public class HelloMethodInterceptor implements MethodInterceptor {
private Object target;
public HelloMethodInterceptor(Object target){
this.target = target;
}
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
System.out.println(“before”+arg1.getName());
最后
毕竟工作也这么久了
必看视频!获取2024年最新Java开发全套学习资料 备注Java
,除了途虎一轮,也七七八八面试了不少大厂,像阿里、饿了么、美团、滴滴这些面试过程就不一一写在这篇文章上了。我会整理一份详细的面试过程及大家想知道的一些问题细节
美团面试经验
[外链图片转存中…(img-c3Oeu6ZZ-17105359391)]
唯品会面试经验
[外链图片转存中…(img-xvz7hNdr-17105359391)]
因篇幅有限,图文无法详细发出