matcha923
金长
2019-05-22 11:47
采纳率: 63.6%
浏览 490
已采纳

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条回答 默认 最新

  • qq_39249094
    已采纳

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

    点赞 评论
  • weixin_39606236
    ꧁gaoKuo꧂ 2019-05-22 11:55

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

    点赞 评论

相关推荐