在 spring 中使用 bean.xml 时出现 Classdefnotfound 异常

Classdefnotfound exception while using bean.xml in spring

org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@fb509a: startup date [Fri Jul 17 21:34:24 IST 2015]; root of context hierarchy Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/OrderComparator$OrderSourceProvider at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:200) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:126) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452) at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:140) at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:84) at mySimpleSpringApp.myApp.main(myApp.java:14) Caused by: java.lang.ClassNotFoundException: org.springframework.core.OrderComparator$OrderSourceProvider at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 7 more

我的主要 class ::

 package mySimpleSpringApp;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;

    public class myApp {
      public static void main(String[] args) {
            ApplicationContext appContext = new FileSystemXmlApplicationContext("appContext.xml");

            Fruit f = appContext.getBean("fruit", Fruit.class);
            Vegetable v = (Vegetable)appContext.getBean("vegetable");

            System.out.println(f.talkAboutYourself());
            System.out.println(v.talkAboutYourself());

        }

    }

bean xml 文件 :: appContext.xml::

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="fruit" class="mySimpleSpringApp.Fruit"></bean>
<bean id="vegetable" class="mySimpleSpringApp.Vegetable" />

</beans>

我做错了什么?

这个问题可能重复,但我没有从其他 post 那里得到答案,因为这些解决方案对我不起作用。

JVM 在运行时尝试加载 class 路径中不存在的 class 时抛出 NoClassDefFoundError

检查 class 是否出现在 class 路径中。

可能是jar没有添加到正确的位置,或者在class路径中没有正确引用,或者jar版本不对。

注意 OrderSourceProvider 自 spring 4.1 以来就存在。检查运行时加载的 jar 是否比该版本旧。