java.lang.ClassCastException的含义:someClass与someClass不兼容 [英] Meaning of java.lang.ClassCastException: someClass incompatible with someClass

查看:185
本文介绍了java.lang.ClassCastException的含义:someClass与someClass不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XPage应用程序中遇到偶然的异常:

I was experiencing occasional exceptions in XPages application:

java.lang.ClassCastException: someClass incompatible with someClass.

两个提到的类都是相同的,它是用作会话bean的类。我无法谷歌任何覆盖我的问题。对此的通常解释是设计元素的变化,而不是我的情况。

Both mentioned classes are the same, it is class used as session bean. I was not able to google anything covering my problem. Usual explanation for this was change in design elements, not my case.

从那时起,XPage应用程序变得无法使用(使用会话bean someClass的页面),直到重启http任务,或者是faces-config.xml的重新保存。

The XPage application become unusable (pages using session bean someClass) since that moment, until restart of http task, or resave of faces-config.xml.

在某些情况下,这与其他例外有关:

In some cases this is related to other exception:

com.ibm.jscript.InterpretException: Script interpreter error, line=x, col=y: 
Java method 'method(signature containg someClass)'
on java class 'someOtherClass' not found

此行为背后是什么?

推荐答案

Philippe Riand通过电子邮件解释了这一点:

Philippe Riand explaned this by email:


此类强制转换是因为同一班级由两个不同的班级装载机装载两次。因此,从Java的角度来看,它们是不同的,并且转换失败。

This class cast happens because the same class had been loaded twice by 2 different class loaders. Thus, from a Java standpoint, they are different and the cast fails.

现在,每个XPages应用程序都有自己的类加载器。但是,每次通过Domino Designer进行设计更改时,都会丢弃此类加载器。这是必需的,因为对XPages的更改会生成一个新的Java类,然后应该加载而不是之前的Java类。发生这种情况时,将丢弃类加载器并创建一个新的。然后,所有与应用程序相关的类都会在需要时重新加载,即使它们没有更改。这是J2EE服务器实现的常见行为。
也就是说,如果您的代码缓存了在设计更改发生时 not 被丢弃的范围内的对象,则可能会发生这种情况。例如,applicationScope&当设计更改发生时,sessionScope当前不会被丢弃,这可能会导致此问题。这是一个设计选择,因为丢弃范围有时会提供糟糕的开发人员体验,但有这个缺点。

Now, each XPages application is having its own classloader. But this class loader is discarded each time a design change happens to the application, through Domino Designer for example. This is required as a change to an XPages generates a new Java class that should then be loaded instead of the previous one. When this happens, the classloader is discarded and a new is created. Then all the application related classes are reloaded, as they are needed, even though they didn't change.This is a common behavior implemented by J2EE servers. That said, if your code is caching an object in a scope that is not discarded when a design change occurs, then this is likely to happen. For example, the applicationScope & sessionScope are currently not discarded when a design change happens, which might lead to this problem. This was a design choice as discarding the scopes sometimes provides a bad developer experience, but with this drawback.

最后,保存faces-config.xml可以解决此问题。保存此文件后,整个模块将从内存中丢弃,包括范围,这就解释了它的工作原理。对自定义Java类进行更改应重新加载模块并删除问题。

Finally, saving faces-config.xml works as a workaround. When this file is saved, then the entire module is discarded from memory, including the scopes, This explains why it works. Making a change to your custom Java class should reload the module and remove the issue.

因此,似乎将bean(甚至是间接的)放入sessionScope或applicationScope是原因。

So it seems putting beans (even indirectly) into sessionScope or applicationScope is the cause.

这篇关于java.lang.ClassCastException的含义:someClass与someClass不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆