如何为同一类的Class对象可以在Java“类”回归面值不同的实例? [英] How can the java 'class' literal return different instances of the Class object for the same class?

查看:221
本文介绍了如何为同一类的Class对象可以在Java“类”回归面值不同的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我运行一个小的Spring MVC应用程序,我在那里利用的 RequestMapping 的注释我控制器上的 AnsController 的。当RequestDispatcher的是扫描我的豆子为的 RequestMapping 的注释,在某些时候它归结到这一行:

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和散列code,则该Class对象存储在注释的阵列上的 AnsController.class 的!

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的,我放在code以上行,我可以看到存储在注释的阵列和的返回的类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).

在换句话说,调用的 RequestMapping.class 的,我在不同的Class对象测试Servlet的效果比完全相同调用同一个code中的的RequestDispatcher 的servlet的。

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.

如果这是甚至可能,假设正在使用相同的类加载器?这是充足的证据得出结论,认为应该重新present同一个注释的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 :).

在我写的,我用与部署在原地应用程序启动码头6服务器的Eclipse插件的时间。该插件放在web项目的所有通过命令行 -classpath 开关建立在AppClasspath依赖。这是可取的,我使用它,应用B / C它让我大大简化在开发模式的类加载策略的具体案例。在这种情况下,然而,我结束了在春天罐子 AppClassLoader WebClassLoader ,二/三春罐子均 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.

一旦我意识到,我不得不设置 parentLoaderPriority WebAppContext 在我的码头配置和问题走了。它仍然是当然的一个黑客,但对于快速和肮脏的应用我在这里做足够好了。

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.

感谢您的所有有用的答复!

Thank you for all the helpful responses!

这篇关于如何为同一类的Class对象可以在Java“类”回归面值不同的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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