Classdefnotfound exception while using bean.xml in spring

陌路散爱 提交于 2019-12-11 12:26:40

问题


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

my main 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 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>

What i am doing wrong here?

This question might be duplicate but i did not get answer from other post, as those solutions did not work for me.


回答1:


The NoClassDefFoundError is thrown by JVM at runtime when it try to load a class that is not present in the classpath.

Check if the class in present in the classpath or not.

May be a jar is not added in the right position or it is not correctly referenced in the classpath, or the jar version is not the right one.

Note the OrderSourceProvider is present since spring 4.1. Check if the jar loaded at runtime is older than that version.



来源:https://stackoverflow.com/questions/31480234/classdefnotfound-exception-while-using-bean-xml-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!