Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

junit测试CGLibproxy动态代理报空指针,不知道是什么原因 #9

Open
dinglianju opened this issue Jan 25, 2018 · 2 comments

Comments

@dinglianju
Copy link

image

@zhoujin7
Copy link

zhoujin7 commented Jul 9, 2021

我搞定了, 如下截图所示, 原因稍后再说

image

@zhoujin7
Copy link

zhoujin7 commented Jul 10, 2021

因为SomeService的BeanDefinition是基于this.enhancer.create().getClass()创建的,
但是基于该BeanDefinition创建的代理类并没有初始化 private InvocationHandler CGLIB$CALLBACK_1;字段,
该字段为null, 当执行this.CGLIB$CALLBACK_1.invoke(this, CGLIB$get2$1, new Object[] { paramString });会报空指针异常

修改createProxyClass方法

private void createProxyClass(Class<?> cls) throws NoSuchMethodException {
	log.info("\tcreate proxy for class:{}", cls);
	final RestInfo restInfo = extractRestInfo(cls);

	MyInvocationHandler handler = getMyInvocationHandler(restInfo);

	boolean proxyClass = isProxyClass(cls);

	if (proxyClass) {
		// 直接registerSingleton
		this.defaultListableBeanFactory.registerSingleton(cls.getSimpleName(), getCGLibBean(cls,handler));
	}else{
		// 创建动态代理类定义
		BeanDefinition beanDefinition = getProxyBeanDefinition(cls, handler);
		registerBeanDefinition(cls, beanDefinition);
	}
}

private Object getCGLibBean(Class<?> cls, MyInvocationHandler handler) {
	CallbackHelper callbackHelper = getCallbackFilter(cls, handler);

	CGLibProxyCreater cgLibProxyCreater = new CGLibProxyCreater(cls, callbackHelper);

	return cgLibProxyCreater.newProxyInstance();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants