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

时间:2015-07-17 16:15:08

标签: java spring spring-mvc spring-bean

  

org.springframework.context.support.AbstractApplicationContext   prepareRefresh INFO:刷新   org.springframework.context.support.FileSystemXmlApplicationContext@fb509a:   启动日期[Fri Jul 17 21:34:24 IST 2015];上下文层次结构的根   线程" main"中的例外情况java.lang.NoClassDefFoundError:   org / springframework / core / OrderComparator $ OrderSourceProvider at   org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:200)     在   org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:126)     在   org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)     在   org.springframework.context.support.FileSystemXmlApplicationContext。(FileSystemXmlApplicationContext.java:140)     在   org.springframework.context.support.FileSystemXmlApplicationContext。(FileSystemXmlApplicationContext.java:84)     在mySimpleSpringApp.myApp.main(myApp.java:14)引起:   抛出java.lang.ClassNotFoundException:   org.springframework.core.OrderComparator $ OrderSourceProvider at   java.net.URLClassLoader.findClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)at   sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)... 7更多

我的主要课程::

 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 file :: 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>

我在这里做错了什么?

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

1 个答案:

答案 0 :(得分:2)

当JVM尝试加载类路径中不存在的类时,JVM会在运行时抛出NoClassDefFoundError

检查类路径中是否存在类。

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

注意自{4.1}春季以来OrderSourceProvider存在。检查运行时加载的jar是否早于该版本。