目标对象: target
代理对象: poxy
外部调用 say1()时会在say2() 中报空指针,很奇怪为什么 在say1() 中 targetBean = poxy,但是say2()
中 targetBean = null
@Component
public class TargetBean {
@Autowired
private TargetBean targetBean;
public void say1(){
targetBean.say2();
}
private void say2(){
targetBean.say3(); //这里会报空指针
}
@Transactional
public void say3(){
}
}
cligb 代理 实际是通过 字节码技术 生成一个子类
public class TargetBean$$EnhancerByCGLIB$$debe56be extends TargetBean implements Factory{
....
}
我们知道 cligb 是不会代理 private,faint 方法的,不会代理也就进入不到callback() 方法(可以理解这就是增强方法),spring 的 bean 是放在增强方法,没有进入callback 就
无法用bean 去调用,say2 调用实际是 直接调poxy的 say2
下面我模拟一下spring cligb 代理可能会更加清楚一些
public class TargetBean {
private TargetBean targetBean;
public TargetBean getTargetBean() {
return targetBean;
}
public void setTargetBean(TargetBean targetBean) {
this.targetBean = targetBean;
}
public void say1(){
targetBean.say2();
}
private void say2(){
targetBean.say3(); //这里会报空指针
}
public void say3(){
}
}
public class TestMain {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//写入磁盘
//System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "E:\\JavaApp");
//1.创建增强类
MyMethodInterceptor myMethodInterceptor = new MyMethodInterceptor();
TargetBean targetBean = new TargetBean();
myMethodInterceptor.setTargetBean(targetBean);
//创建cligb 代理
Enhancer enhancer = new Enhancer();
enhancer.setCallback(myMethodInterceptor);
enhancer.setSuperclass(TargetBean.class);
TargetBean proxy = (TargetBean)enhancer.create();
// 把代理设置到目标类,相等于 spring 的
// @Autowired
// private TargetBean targetBean;
targetBean.setTargetBean(proxy);
proxy.say1();
}
//MyMethodInterceptor 参照 ReflectiveMethodInvocation 简化编写的
static class MyMethodInterceptor implements MethodInterceptor {
private TargetBean targetBean;
public TargetBean getTargetBean() {
return targetBean;
}
public void setTargetBean(TargetBean targetBean) {
this.targetBean = targetBean;
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println(method.getName()+"进入增强方法");
Object obj = method.invoke(targetBean,objects);
return obj;
}
}
}
执行poxy.say1() 先会进入 intercept(),然后在反射调用 targetBean 的 say1()
然后 say1() 调用 targetBean.say2(); 此时是在targetBean中运行的因此 targetBean =prox
因为 say2()是private,所以会直接调用 poxy父类的say2(),
在poxy中say2() 调用targetBean.say3(),因为 poxy 中的 targetBean 是空的,所以会报空指针
最后把cligb 在内存中生成的字节码,写入磁盘看看。。。。
public class TargetBean$$EnhancerByCGLIB$$b718d8ed extends TargetBean implements Factory {
private boolean CGLIB$BOUND;
public static Object CGLIB$FACTORY_DATA;
private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
private static final Callback[] CGLIB$STATIC_CALLBACKS;
private MethodInterceptor CGLIB$CALLBACK_0;
private static Object CGLIB$CALLBACK_FILTER;
private static final Method CGLIB$setTargetBean$0$Method;
private static final MethodProxy CGLIB$setTargetBean$0$Proxy;
private static final Object[] CGLIB$emptyArgs;
private static final Method CGLIB$say1$1$Method;
private static final MethodProxy CGLIB$say1$1$Proxy;
private static final Method CGLIB$getTargetBean$2$Method;
private static final MethodProxy CGLIB$getTargetBean$2$Proxy;
private static final Method CGLIB$say3$3$Method;
private static final MethodProxy CGLIB$say3$3$Proxy;
private static final Method CGLIB$equals$4$Method;
private static final MethodProxy CGLIB$equals$4$Proxy;
private static final Method CGLIB$toString$5$Method;
private static final MethodProxy CGLIB$toString$5$Proxy;
private static final Method CGLIB$hashCode$6$Method;
private static final MethodProxy CGLIB$hashCode$6$Proxy;
private static final Method CGLIB$clone$7$Method;
private static final MethodProxy CGLIB$clone$7$Proxy;
static void CGLIB$STATICHOOK1() {
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
CGLIB$emptyArgs = new Object[0];
Class var0 = Class.forName("com.eyedsion.his.third.workInjury.inp.pojo.TargetBean$$EnhancerByCGLIB$$b718d8ed");
Class var1;
Method[] var10000 = ReflectUtils.findMethods(new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods());
CGLIB$equals$4$Method = var10000[0];
CGLIB$equals$4$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$4");
CGLIB$toString$5$Method = var10000[1];
CGLIB$toString$5$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$5");
CGLIB$hashCode$6$Method = var10000[2];
CGLIB$hashCode$6$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$6");
CGLIB$clone$7$Method = var10000[3];
CGLIB$clone$7$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$7");
var10000 = ReflectUtils.findMethods(new String[]{"setTargetBean", "(Lcom/eyedsion/his/third/workInjury/inp/pojo/TargetBean;)V", "say1", "()V", "getTargetBean", "()Lcom/eyedsion/his/third/workInjury/inp/pojo/TargetBean;", "say3", "()V"}, (var1 = Class.forName("com.eyedsion.his.third.workInjury.inp.pojo.TargetBean")).getDeclaredMethods());
CGLIB$setTargetBean$0$Method = var10000[0];
CGLIB$setTargetBean$0$Proxy = MethodProxy.create(var1, var0, "(Lcom/eyedsion/his/third/workInjury/inp/pojo/TargetBean;)V", "setTargetBean", "CGLIB$setTargetBean$0");
CGLIB$say1$1$Method = var10000[1];
CGLIB$say1$1$Proxy = MethodProxy.create(var1, var0, "()V", "say1", "CGLIB$say1$1");
CGLIB$getTargetBean$2$Method = var10000[2];
CGLIB$getTargetBean$2$Proxy = MethodProxy.create(var1, var0, "()Lcom/eyedsion/his/third/workInjury/inp/pojo/TargetBean;", "getTargetBean", "CGLIB$getTargetBean$2");
CGLIB$say3$3$Method = var10000[3];
CGLIB$say3$3$Proxy = MethodProxy.create(var1, var0, "()V", "say3", "CGLIB$say3$3");
}
final void CGLIB$setTargetBean$0(TargetBean var1) {
super.setTargetBean(var1);
}
public final void setTargetBean(TargetBean var1) {
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$setTargetBean$0$Method, new Object[]{var1}, CGLIB$setTargetBean$0$Proxy);
} else {
super.setTargetBean(var1);
}
}
final void CGLIB$say1$1() {
super.say1();
}
public final void say1() {
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$say1$1$Method, CGLIB$emptyArgs, CGLIB$say1$1$Proxy);
} else {
super.say1();
}
}
final TargetBean CGLIB$getTargetBean$2() {
return super.getTargetBean();
}
public final TargetBean getTargetBean() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? (TargetBean)var10000.intercept(this, CGLIB$getTargetBean$2$Method, CGLIB$emptyArgs, CGLIB$getTargetBean$2$Proxy) : super.getTargetBean();
}
final void CGLIB$say3$3() {
super.say3();
}
public final void say3() {
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$say3$3$Method, CGLIB$emptyArgs, CGLIB$say3$3$Proxy);
} else {
super.say3();
}
}
final boolean CGLIB$equals$4(Object var1) {
return super.equals(var1);
}
public final boolean equals(Object var1) {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
Object var2 = var10000.intercept(this, CGLIB$equals$4$Method, new Object[]{var1}, CGLIB$equals$4$Proxy);
return var2 == null ? false : (Boolean)var2;
} else {
return super.equals(var1);
}
}
final String CGLIB$toString$5() {
return super.toString();
}
public final String toString() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$5$Method, CGLIB$emptyArgs, CGLIB$toString$5$Proxy) : super.toString();
}
final int CGLIB$hashCode$6() {
return super.hashCode();
}
public final int hashCode() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
Object var1 = var10000.intercept(this, CGLIB$hashCode$6$Method, CGLIB$emptyArgs, CGLIB$hashCode$6$Proxy);
return var1 == null ? 0 : ((Number)var1).intValue();
} else {
return super.hashCode();
}
}
final Object CGLIB$clone$7() throws CloneNotSupportedException {
return super.clone();
}
protected final Object clone() throws CloneNotSupportedException {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? var10000.intercept(this, CGLIB$clone$7$Method, CGLIB$emptyArgs, CGLIB$clone$7$Proxy) : super.clone();
}
public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
String var10000 = var0.toString();
switch(var10000.hashCode()) {
case -1063877872:
if (var10000.equals("getTargetBean()Lcom/eyedsion/his/third/workInjury/inp/pojo/TargetBean;")) {
return CGLIB$getTargetBean$2$Proxy;
}
break;
case -508378822:
if (var10000.equals("clone()Ljava/lang/Object;")) {
return CGLIB$clone$7$Proxy;
}
break;
case -222593168:
if (var10000.equals("setTargetBean(Lcom/eyedsion/his/third/workInjury/inp/pojo/TargetBean;)V")) {
return CGLIB$setTargetBean$0$Proxy;
}
break;
case 1826985398:
if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
return CGLIB$equals$4$Proxy;
}
break;
case 1873981455:
if (var10000.equals("say1()V")) {
return CGLIB$say1$1$Proxy;
}
break;
case 1874041037:
if (var10000.equals("say3()V")) {
return CGLIB$say3$3$Proxy;
}
break;
case 1913648695:
if (var10000.equals("toString()Ljava/lang/String;")) {
return CGLIB$toString$5$Proxy;
}
break;
case 1984935277:
if (var10000.equals("hashCode()I")) {
return CGLIB$hashCode$6$Proxy;
}
}
return null;
}
public TargetBean$$EnhancerByCGLIB$$b718d8ed() {
CGLIB$BIND_CALLBACKS(this);
}
public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) {
CGLIB$THREAD_CALLBACKS.set(var0);
}
public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) {
CGLIB$STATIC_CALLBACKS = var0;
}
private static final void CGLIB$BIND_CALLBACKS(Object var0) {
TargetBean$$EnhancerByCGLIB$$b718d8ed var1 = (TargetBean$$EnhancerByCGLIB$$b718d8ed)var0;
if (!var1.CGLIB$BOUND) {
var1.CGLIB$BOUND = true;
Object var10000 = CGLIB$THREAD_CALLBACKS.get();
if (var10000 == null) {
var10000 = CGLIB$STATIC_CALLBACKS;
if (CGLIB$STATIC_CALLBACKS == null) {
return;
}
}
var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0];
}
}
public Object newInstance(Callback[] var1) {
CGLIB$SET_THREAD_CALLBACKS(var1);
TargetBean$$EnhancerByCGLIB$$b718d8ed var10000 = new TargetBean$$EnhancerByCGLIB$$b718d8ed();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Callback var1) {
CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
TargetBean$$EnhancerByCGLIB$$b718d8ed var10000 = new TargetBean$$EnhancerByCGLIB$$b718d8ed();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
CGLIB$SET_THREAD_CALLBACKS(var3);
TargetBean$$EnhancerByCGLIB$$b718d8ed var10000 = new TargetBean$$EnhancerByCGLIB$$b718d8ed;
switch(var1.length) {
case 0:
var10000.<init>();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
default:
throw new IllegalArgumentException("Constructor not found");
}
}
public Callback getCallback(int var1) {
CGLIB$BIND_CALLBACKS(this);
MethodInterceptor var10000;
switch(var1) {
case 0:
var10000 = this.CGLIB$CALLBACK_0;
break;
default:
var10000 = null;
}
return var10000;
}
public void setCallback(int var1, Callback var2) {
switch(var1) {
case 0:
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;
default:
}
}
public Callback[] getCallbacks() {
CGLIB$BIND_CALLBACKS(this);
return new Callback[]{this.CGLIB$CALLBACK_0};
}
public void setCallbacks(Callback[] var1) {
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0];
}
static {
CGLIB$STATICHOOK1();
}
}