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

时间:2016-08-24 11:37:35

标签: java spring maven

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

enter image description here

的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>

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>

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);
    }
}

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();
    }
}

这给了我以下错误:

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

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

5 个答案:

答案 0 :(得分:11)

我也得到了同样的错误,并在添加spring-expression lib

后得到修复

答案 1 :(得分:4)

我在eclipse中测试了你的代码,它在4.2.2.RELEASE和4.3.2.RELEASE下运行良好。它正在打印下面的消息。可以请你清理你从命令提示符构建或运行构建。

Spring  : Hello ! Mahesha999

答案 2 :(得分:0)

只有在没有&#34; spring-expression&#34;时才会出现此错误。库。
解决方案1: 如果添加spring-expression lib后仍然存在,则尝试添加新版本的spring库。

解决方案2: 删除spring.xml文件及其packg,然后使用spring.xml创建新包。

答案 3 :(得分:0)

i have use spring 4.3.11 version

so add the bellow jar into lib folder

1. commons-logging-1.1.1.jar
2. servlet-api.jar
3. spring-aop-4.3.11.RELEASE.jar
4. spring-beans-4.3.11.RELEASE.jar
5. spring-context-4.3.11.RELEASE.jar
6.spring-core-4.3.11.RELEASE.jar
7. spring-expression-4.3.11.RELEASE.jar
8. spring-web-4.3.11.RELEASE.jar
9.spring-webmvc-4.3.11.RELEASE.jar

答案 4 :(得分:0)

解决方案:在您的项目中添加“ spring-expression-4.3.11.RELEASE.jar”并重建您的项目。