java.lang.ClassNotFoundException:org.springframework.expression.ParserContext Spring HelloWorld?

Mah*_*aha 8 java spring maven

我有简单的Spring HelloWorld程序.其内容如下:

在此输入图像描述

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mahesha999.examples.spring</groupId>
    <artifactId>SpringExamples</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>       
    </dependencies>

</project>
Run Code Online (Sandbox Code Playgroud)

eg1.xml

<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-4.0.xsd">

    <bean id="helloBean" class="com.mahesha999.examples.spring.eg1.HelloWorld">
        <property name="name" value="Mahesha999" />
    </bean>

</beans>
Run Code Online (Sandbox Code Playgroud)

HelloWorld.java

public class HelloWorld {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public void printHello() {
        System.out.println("Spring  : Hello ! " + name);
    }
}
Run Code Online (Sandbox Code Playgroud)

App.java

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

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

        HelloWorld obj = (HelloWorld) context.getBean("helloBean");
        obj.printHello();
    }
}
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

INFO: Loading XML bean definitions from class path resource [eg1.xml]
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
    at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:628)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:516)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.mahesha999.examples.spring.eg1.App.main(App.java:8)
Caused by: java.lang.ClassNotFoundException: org.springframework.expression.ParserContext
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 5 more
Run Code Online (Sandbox Code Playgroud)

当我在pom.xml中将spring version从4.3.2更改为4.2.2时,它运行正常.为什么会这样?

Pro*_*ver 12

我也得到了相同的错误,并在添加spring-expression lib后得到修复


aba*_*hel 4

我在 Eclipse 中测试了您的代码,它在 4.2.2.RELEASE 和 4.3.2.RELEASE 上都运行良好。它正在打印下面的消息。您能否清理您的构建或从命令提示符运行构建。

Spring  : Hello ! Mahesha999
Run Code Online (Sandbox Code Playgroud)

  • 您可以尝试从本地 .m2 存储库中删除文件,也可以在另一台计算机上构建并运行您的项目。正如我所说,这个项目的两个版本都对我来说运行良好。 (2认同)