gpt4 book ai didi

java - 找不到 spring 类路径资源,因为它不存在

转载 作者:行者123 更新时间:2023-11-30 03:39:43 25 4
gpt4 key购买 nike

我使用我的第一个 spring helloworld 程序,使用 STS 使用 Maven,这是一个简单的 IOC 示例

public class Soloapp {
String solo;
public Soloapp() {
// TODO Auto-generated constructor stub
}
public String getSolo() {
return solo;
}
public void setSolo(String solo) {
this.solo = solo;
}

}

和实现类

package com.solo.spring;

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

public class Soloimp {

public static void main(String[] args) {
ApplicationContext ap= new ClassPathXmlApplicationContext("/WEB-INF/spring/root-context.xml");
Soloapp apa= (Soloapp) ap.getBean("solo");
System.out.println(apa.getSolo());


}

}

并获取异常

l

og4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [WEB-INF/spring/root-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/spring/root-context.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.solo.spring.Soloimp.main(Soloimp.java:9)

它显示找不到 xml 文件位置,但我指定了我使用 spring 工具套件 mvc maven 项目的位置

项目结构如下 enter image description here

最佳答案

您可以尝试 Vaibs 在链接答案中建议的内容,但为此您必须将 root-context.xml 移动到 src/main/resources 中。

如果你想将其保持在适当的位置,请像这样加载

ConfigurableApplicationContext ap = new ClassPathXmlApplicationContext("file:src/main/webapp/WEB-INF/spring/root-context.xml");

关于java - 找不到 spring 类路径资源,因为它不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27056319/

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