java 'class' 文字如何为同一个类返回 Class 对象的不同实例? [英] How can the java 'class' literal return different instances of the Class object for the same class?

查看:29
本文介绍了java 'class' 文字如何为同一个类返回 Class 对象的不同实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常令人费解的情况,正在寻找任何想法.

I have an extremely puzzling situation and looking for any ideas.

我正在运行一个小型 Spring MVC 应用程序,我在我的控制器AnsController"上使用了 RequestMapping 注释.当 RequestDispatcher 为 RequestMapping 注释扫描我的 bean 时,在某些时候它归结为这一行:

I'm running a small Spring MVC app, where I make use of the RequestMapping annotation on my controller "AnsController". When RequestDispatcher is scanning my beans for the RequestMapping annotation, at some point it comes down to this line:

clazz.getAnnotation(RequestMapping.class)

(clazz = AnsController.class)

(clazz = AnsController.class)

上面的行没有找到注释,即使它在那里.

The line above does not find the annotation, even though it is there.

我开始在 Eclipse 调试器中对此进行调查,发现了一个非常令人费解的问题.上述行失败的原因是 b/c RequestMapping.class 正在返回一个 Class 对象,该对象似乎描述了正确的注释,但内部 id 和 hashCode 与存储在 <AnsController.class 上的 em>annotations 数组!

I started investigating this in Eclipse debugger and found a very puzzling problem. The reason why the above line fails is b/c RequestMapping.class is returning a Class object which seems to describe the correct annotation, but has a different internal id and hashCode then the Class object stored in the annotations array on the AnsController.class!

我写了一个测试servlet,我在上面放了这行代码,可以看到annotations数组中存储的Class和RequestMapping.class 是同一个对象.

I wrote a test servlet where I placed the above line of code, and I can see that the Class stored in the annotations array and the Class returned by RequestMapping.class are the same object.

然而在 RequestDispatcher servlet 中,RequestMapping.class 似乎为同一个注释实例化了另一个类的实例(我可以告诉 b/c 内部 id高于注释映射中Class对象的id).

Yet in the RequestDispatcher servlet, RequestMapping.class seems to instantiate another instance of the Class for that same annotation (I can tell b/c the internal id is much higher than the id of the Class object in the annotations map).

换句话说,与在 RequestDispatcher servlet 中调用完全相同的代码相比,在我的测试 Servlet 中调用 RequestMapping.class 会产生不同的 Class 对象.

In other words, calling RequestMapping.class in my Test Servlet results in a different Class object than calling exactly the same code in RequestDispatcher servlet.

假设正在使用相同的类加载器,这甚至可能吗?这是否足以证明 Class 对象的这些不同实例必须由不同的类加载器生成,这些实例应该表示同一个注释?

Should this be even possible, assuming the same classloader is being used? Is this sufficient evidence to conclude that these distinct instances of the Class object that are supposed to represent one and the same annotation must be produced by different classloaders?

我找不到任何书面材料可以证实我的假设,即每个类只允许一个 Class 对象实例,但这似乎是合理的……还是我错了?

I can't find anything in writing that would confirm my assumption that only one instance of Class object per class is allowed, but it seems reasonable... Or am I wrong?

推荐答案

我们有了.在被这里的所有答案说服我有一个类加载器混乱之后,我能够找到问题.一年前的一次黑客攻击让我大吃一惊:).

And there we have it. After being convinced by all the answers here that I have a classloader mess, I was able to find the problem. I was bit in the ass by a hack I did a year ago :).

当时我编写了一个 Eclipse 插件,我用它来启动 Jetty 6 服务器,应用程序就地部署.该插件通过命令行 -classpath 开关将所有 Web 项目构建依赖项放置在 AppClasspath 上.这对于我使用它的应用程序来说是可取的,b/c 它允许我极大地简化开发模式下针对特定情况的类加载策略.然而,在这种情况下,我最终在 AppClassLoaderWebClassLoader 上使用 Spring jar,b/c Spring jar 位于 WEB-INF/lib.

At the time I wrote an Eclipse plugin that I used to launch the Jetty 6 server with the app deployed in-place. That plugin placed all of the web project build dependencies on the AppClasspath via the commandline -classpath switch. This was desirable for the app I was using it for, b/c it allowed me to greatly simplify the classloading strategy in development mode for that specific case. In this case, however, I ended up with Spring jars on the AppClassLoader and WebClassLoader, b/c the Spring jars were in WEB-INF/lib.

一旦我意识到这一点,我只需在我的码头配置中将 WebAppContext 上的 parentLoaderPriority 设置为 true ,问题就消失了.当然,它仍然是一个 hack,但对于我在这里做的快速而肮脏的应用程序来说已经足够了.

Once I realized that, I just had to set parentLoaderPriority on WebAppContext to true in my jetty config and the problem went away. It's still a hack of course, but good enough for the quick-and-dirty app I'm doing here.

感谢所有有用的回复!

这篇关于java 'class' 文字如何为同一个类返回 Class 对象的不同实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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