Spring基于XML配置时出现报错,但是我的路径应该是对的呀,求大神指导

报错信息

信息: Loading XML bean definitions from class path resource [Appconfig.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Appconfig.xml]; nested exception is java.io.FileNotFoundException: class path resource [Appconfig.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:223)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:194)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:258)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:620)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:521)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    at com.config.RunTest.main(RunTest.java:11)
Caused by: java.io.FileNotFoundException: class path resource [Appconfig.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
    ... 13 more

代码

package com.config;

import com.dao.UserDao;
import com.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RunTest {
    public static void main(String [] args) throws Exception{
        ApplicationContext ctx = new ClassPathXmlApplicationContext("Appconfig.xml");
//        ApplicationContext ctx=new AnnotationConfigApplicationContext(Appconfig.class);
        UserService userService=(UserService) ctx.getBean("userService");
        userService.login("aaa","bbb");
        UserDao userDao=(UserDao) ctx.getBean("userDao");
        userDao.login("aaa","bbb");
    }
}

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.xsd">
  <!--通过类的全限定名来声明要创建的bean-->
    <bean id="userDao" class="com.dao.UserDaoImpl"></bean>
  <!--通过类的全限定名来声明要创建的bean-->
    <bean id="userService" class="com.service.UserServiceImpl">
        <!--配置依赖注入-->
        <constructor-arg ref="userDao"></constructor-arg>
    </bean>
</beans>

感谢指导!!!

2个回答

你的xml文件位置是不是放错了

matcha923
金长 ...我用的IDEA,要把XML文件放在resources这个里面...谢谢啦
大约一年之前 回复

项目构建一下,看看编译后Appconfig.xml路径在哪

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐