Spring - 从类路径资源[Beans.xml]加载XML bean定义 - NoClassDefFoundError

时间:2015-05-26 03:55:00

标签: java xml spring spring-mvc

我是Spring的新手,我在第一个Spring项目中指的是this tutorial。按照所有说明操作后,我尝试运行程序,但是我遇到了这个错误。

May 26, 2015 11:42:45 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@31221be2: startup date [Tue May 26 11:42:45 CST 2015]; root of context hierarchy
May 26, 2015 11:42:45 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
    at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:553)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.tutorialspoint.MainApp.main(MainApp.java:9)
Caused by: java.lang.ClassNotFoundException: org.springframework.expression.ParserContext
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    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)
    ... 5 more

知道我做错了什么。据我所知,我正在正确地学习本教程。

CODE

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");

      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

      obj.getMessage();
   }
}

的beans.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-3.0.xsd">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

3 个答案:

答案 0 :(得分:12)

检查您是否在类路径中添加了所有必需的jar。

org.springframework.expression.ParserContext位于spring-expresssion-YOUR_SPRING_VERSION.jar内。检查是否在类路径中添加了此jar。

答案 1 :(得分:0)

我遇到了同样的问题。但我发现了我的错误。

我的错误是,我在com.tutorialspont包中创建了&#34; Beans.xml&#34;

我的解决方案是&#34; Beans.xml&#34; 必须直接在 src文件夹下创建。

答案 2 :(得分:-1)

log4j:WARN找不到logger(org.springframework.core.env.StandardEnvironment)的appender。 log4j:WARN请正确初始化log4j系统。 线程&#34; main&#34;中的例外情况 java.lang.NoClassDefFoundError:org / springframework / expression / PropertyAccessor     在org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory (AbstractApplicationContext.java:553)     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)     在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)     在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)     在com.javahonk.client.TestWebService.main(TestWebService.java:19) 引起:java.lang.ClassNotFoundException:org.springframework.expression.PropertyAccessor     在java.net.URLClassLoader $ 1.run(URLClassLoader.java:372)     在java.net.URLClassLoader $ 1.run(URLClassLoader.java:361)     at java.security.AccessController.doPrivileged(Native Method)     在java.net.URLClassLoader.findClass(URLClassLoader.java:360)     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)     at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)     ......还有5个

上述问题的解决方案:如果类路径中没有spring-expression-xxx.Release.jar,则会发生这种情况。请在类路径中添加spring-expression-3.2.3.RELEASE或最新版本的jar来解决此问题。