gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:13:24 25 4
gpt4 key购买 nike

我是 Spring 的新手,我指的是 this tutorial对于我的第一个 Spring 项目。按照所有说明操作后,我尝试运行该程序,但出现此错误。

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

不知道我哪里做错了。据我所知,我正确地遵循了教程。

代码

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>

最佳答案

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

org.springframework.expression.ParserContext 类位于 spring-expresssion-YOUR_SPRING_VERSION.jar 中,检查您是否已将此 jar 添加到类路径中。

关于java - Spring - 从类路径资源 [Beans.xml] 加载 XML bean 定义 - NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30449191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com