Spring java.lang.LinkageError:违反加载程序约束:加载程序先前已启动名称为X的其他类型的加载 [英] Spring java.lang.LinkageError: loader constraint violation: loader previously initiated loading for a different type with name X

查看:386
本文介绍了Spring java.lang.LinkageError:违反加载程序约束:加载程序先前已启动名称为X的其他类型的加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring的新手,并使用Spring 3.2.2.我有一些通过<constructor-arg>注入的豆,效果很好.现在,我想通过@Autowired注入一些完全出错的bean.我已经做到了:

I am new to Spring and use Spring 3.2.2. I have some beans which I injected via <constructor-arg> which works fine. Now I wanted to inject some bean via @Autowired which totally went wrong. I have done this:

beans.xml:

beans.xml:

<context:annotation-config />
<bean id="formulaFactory" class="my.project.formula.impl.GenericFormulaFactory"
    factory-method="getInstance">
<qualifier value="formulaFactory"></qualifier>
</bean>

Java来源:

@Autowired
@Qualifier("formulaFactory")
private FormulaFactory formulaFactory;

(更改预选赛或将其保留没有任何区别...)

(Changing the qualifier or leaving it out did not make any difference...)

我收到此错误:

java.lang.LinkageError: loader constraint violation: loader (instance of org/springframework/context/support/ContextTypeMatchClassLoader$ContextOverridingClassLoader) previously initiated loading for a different type with name "my/project/formula/FormulaKey"

我想知道为什么会出现此错误.尤其是FormulaKey类型使我感到恼火.当我将@Autowired批注与其他bean一起使用时,它可以工作.

I wonder why this error comes up. Especially the type FormulaKey irritates me. When I use the @Autowired annotation with some other bean, it works.

我不得不提到,我是通过getInstance方法将GenericFormulaFactory实现为单例的.也许会引起一些问题?

I have to mention that I implemented the GenericFormulaFactory as singleton via getInstance method. Maybe that causes some problems?

该应用程序是一个独立的应用程序.我也检查了所有罐子的重复性,我不认为这是问题的原因,因为该错误与我自己的类有关.

The application is a stand-alone app. I checked all the jars for duplicity too and I do not assume that this is the cause of the problem because the error relates to my own classes.

关于, 奥利弗(Oliver)

Regards, Oliver

更新: 我删除了错误,却不知道是什么原因造成的.

UPDATE: I removed the error without knowing what cause it.

我做了什么:

  1. 删除getInstance()方法和工厂实现的单例性质
  2. 将工厂接口添加到处理程序类构造函数(以及xml中的constructor-arg)

现在,我可以使用xml来配置实现,并将其与@Autowired批注一起使用.

Now I can use the xml to configure the implementation and use it with @Autowired annotations too.

xml:

<bean id="formulaHandler" class="my.project.formula.impl.DefaultFormulaHandler">
    <constructor-arg ref="formulaFactory" />
</bean>
<bean id="formulaFactory" class="my.project.formula.impl.GenericFormulaFactory" />

仍然想知道为什么错误首先出现.在工厂的实现中,使用FormulaKey作为键创建了HashMap,因此这可能会引起麻烦.如果有人知道答案,我真的很想知道.

Still wondering why the error came up in the first place. In the implementation of the factory, a HashMap was created using FormulaKey as key, so maybe this caused trouble. If someone knows the answer, I would really like to know it.

推荐答案

到目前为止,我可以收集到以下信息:

Here is what I could gather so far:

  1. 出现错误java.lang.LinkageError的情况是,加载一个类涉及两个类加载器.
  2. 类加载器通过生成唯一标识符来跟踪加载的类,该标识符包含完全限定的类名称本身,并由类加载器加载.
  3. 当类加载器接收到另一个已经由自身加载的类加载的类的引用时,这会导致错误情况,因为一个类在类加载层次结构中只能被加载一次.
  4. 当涉及到自定义类加载器时,为了加载特定的类,实践是如果已经加载了该类,则首先查询父类加载器.
  5. 在这种情况下,当自定义类加载器没有查询父层次结构并决定加载类本身时,可能存在这样的情况,即正在加载的类已经被父层次结构中的某个类加载器加载.
  6. 请阅读Frankie的" The Java.lang. LinkageError:违反装载程序约束条件"mystified " 了解更多信息.
  7. 对于您的情况,我猜XML配置和注释处理是由两个不同的类加载器完成的.
  8. 在错误情况下,my.project.formula.FormulaKey由一个类加载器加载,然后注释处理中涉及的类加载器再加载一次.
  9. 更改代码时,由于从XML加载上下文时不再引用my.project.formula.FormulaKey,因此延迟加载.
  1. The error java.lang.LinkageError comes in a situation when there are two classloaders involved in loading a class.
  2. A classloader keeps track of the classes loaded by generating a unique identifier which contains the fully qualified class name itself and the classloader loaded it.
  3. When a classloader receives a reference of a class loaded by another class which is already loaded by itself, this results into erroneous situation as a class can be loaded only once in a classloading hierarchy.
  4. When custom classloaders are involved, for loading a particular class, the practice is that the parent classloaders are queried first if that class is already loaded.
  5. In the scenario, when a custom classloader does not query the parent hierarchy and decided to load the class itself, there might be case wherein the class being loaded is already loaded by some classloader in parent hierarchy.
  6. Read Kieviet, Frank's "The java.lang.LinkageError: loader constraint violation" demystified" to know more about it.
  7. For your case, I guess the XML configuration and Annotation processing is done by two different classloaders.
  8. In the error scenario, my.project.formula.FormulaKey is loaded by one classloader and then the class loader involved in annotation processing loads it one more time.
  9. When you changed the code, the loading of my.project.formula.FormulaKey is deferred as its no more referred while loading the context from XML .

这篇关于Spring java.lang.LinkageError:违反加载程序约束:加载程序先前已启动名称为X的其他类型的加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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