【问题标题】:HelloWorld Spring application throws Exception in thread "main": java.lang.NoClassDefFoundHelloWorld Spring 应用程序在线程“main”中抛出异常:java.lang.NoClassDefFounderror
【发布时间】:2021-06-15 13:08:23
【问题描述】:

我正在尝试开发一个 Spring“HelloWorld”项目,而我正在运行该应用程序,它给了我这个错误:

INFO:从类路径资源 [Beans.xml] 加载 XML bean 定义线程“main”java.lang.NoClassDefFoundError 异常:org.springframework.expression.ExpressionParser

下面是我的代码:

package com.tutorialspoint;

public class HelloWorld {
    private String message;

    public void getMessage() {
        System.out.println("your message : "+message);
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

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();
    // TODO Auto-generated method stub
    }
}

ApplicationContext.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>

【问题讨论】:

标签: java spring


【解决方案1】:

除了添加所有依赖的 jars 之外,bean 定义中定义的 class 似乎是无效

com.tutorialspoint/HelloWorld
                  ^

成功了,

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

还应将context.getBean("helloWorld ") 更改为context.getBean("helloWorld")

【讨论】:

    【解决方案2】:

    包括

    spring-expression-3.1.1.Release.jar

    lib 文件夹下

    我提到了 3.1.1 作为例子,你可以使用最新版本,如果有的话。

    【讨论】:

      【解决方案3】:

      将 spring 依赖项添加到您的类路径

      【讨论】:

        【解决方案4】:

        如果您不使用 Maven,如 M Sach 所说,请将“spring expression-X.Release.jar”添加到您的“lib”文件夹中,并且不要忘记将其添加到 Eclipse 中的构建路径中

        【讨论】:

          【解决方案5】:

          这个与spring-expression.jar相关的异常添加这个。

          在您的pom.xml 中添加以下依赖项

          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-expression</artifactId>
              <version>5.3.8</version>
          </dependency>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-12-06
            • 1970-01-01
            • 2012-12-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-10-02
            相关资源
            最近更新 更多