在 Spring 中,为什么 POJO 上的 CGLib 的行号会丢失?
Spring MVC Web 应用程序:
我有一个不带行号的堆栈跟踪(显示在底部)。
我认为这是由于 CGLib 在控制器上运行所致。但这对我来说很奇怪,实际的异常发生在 ServerBatchRemoteRequestAcceptor 中,这是一个未注入的 pojo,而不是控制器。它仅在 Controller 对象中创建。
示例:
@Controller
class MyController {
MyPojo pojo = new MyPojo();
@RequestMapping("myaction")
public void doMyAction(){
pojo.methodToCauseNullPointerException()
}
}
java.lang.NullPointerException
at mycommons.services.batchremoteprocessor.ServerBatchRemoteRequestAcceptor.acceptRequest(Unknown Source)
at com.proxyandvpn.web.controllers.RESTServicesController.handleGenericClientRequest(Unknown Source)
at com.proxyandvpn.web.controllers.RESTServicesController$$FastClassByCGLIB$$dff24f0f.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
有人可以向我解释这种行为吗?由于 CGLib,我的控制器下的每个调用都会没有行号吗?
我应该将控制器写入接口以便使用代理吗?这正常吗?我这样做是为了服务,但将控制器作为简单的 POJO 来完成。
Spring MVC web app:
I have a stack trace w/o line numbers (shown at bottom).
I presume that this is due to CGLib running on the controller. But this is odd to me, the actual exception occurs in ServerBatchRemoteRequestAcceptor
, a pojo that is not injected, not the controller. It is only created in the Controller object.
Example:
@Controller
class MyController {
MyPojo pojo = new MyPojo();
@RequestMapping("myaction")
public void doMyAction(){
pojo.methodToCauseNullPointerException()
}
}
java.lang.NullPointerException
at mycommons.services.batchremoteprocessor.ServerBatchRemoteRequestAcceptor.acceptRequest(Unknown Source)
at com.proxyandvpn.web.controllers.RESTServicesController.handleGenericClientRequest(Unknown Source)
at com.proxyandvpn.web.controllers.RESTServicesController$FastClassByCGLIB$dff24f0f.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
Can someone explain this behavior to me? Will every call under my Controllers be without line numbers due to CGLib?
Should I write my Controllers to an interface so that proxies are used? Is that normal? I do it for services, but have done the controllers as simple POJOs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Spring 使用 CGLIB 生成位于某些组件/控制器前面的代理对象。对这些组件的调用通过 CGLIB 代理传递。这些代理是在运行时生成的,没有源代码,因此它们没有行号。
不过,您几乎可以忽略提到 CGLIB 的堆栈跟踪行 - 假装它们不存在,它们应该是透明的。
在堆栈跟踪中,对 RESTServicesController.handleGenericClientRequest 的调用已被代理,但调用仍在到达那里。 NPE 发生在 ServerBatchRemoteRequestAcceptor 内,该接收器是从 RESTServicesController.handleGenericClientRequest 调用的。
但是,您发布的源代码与堆栈跟踪无关,因此很难评论它发生的原因。
Spring uses CGLIB to generate proxy objects that sit in front of some of your components/controllers. Calls to those components pass through the CGLIB proxies. These proxies are generated a runtime, with no source code, so they have no line numbers.
You can pretty much ignore the stack trace lines that mention CGLIB, though - pretend they're not there, they should be transparent.
In your stack trace, the call to
RESTServicesController.handleGenericClientRequest
has been proxied, but the call is still getting there. The NPE is occurring withinServerBatchRemoteRequestAcceptor
, which is being invoked fromRESTServicesController.handleGenericClientRequest
.However, the source code you posted has no relation to the stack trace, so it's hard to comment as to why it happened.
我将添加这个答案,因为我现在知道了我的问题。当你不理解问题时,通常是我没有问正确的问题。
我最近转而使用 ANT 构建而不是 Eclipse 构建器。我没有意识到我需要在任务中明确启用调试信息。当我看到 CGLib 出现这个错误时,我无意中责怪了它,但所需要的只是 ANT 构建修改。
I will add to this answer as I now know my problem. As is common when you don't understand a problem, I didn't ask the right question.
I had recently switched to using ANT builds instead of Eclipse builder. I didn't realize that I needed to expressly enable debugging information in the task. I unwittingly blamed CGLib when I saw it sitting in front of this error, but all that was necessary was an ANT build modification.