Spring:在类路径中找不到配置xml

问题描述 投票:0回答:4

对于我的简单maven项目,这不起作用:

ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");

config.xml居住在同一级别

实际上,如何将config.xml添加到classpath?注意:我的项目是一个lib,如果我在其他web项目中使用web.xml中的配置执行相同操作:

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:config.xml</param-value>
        </context-param>

它运作正常

这里我不需要web.xml,只需更正类路径。

spring classpath filenotfoundexception applicationcontext
4个回答
2
投票

当您输入classpath*:config.xml时,classpath*是一张外卡,表示您要在整个类路径上加载与config.xml匹配的每个文件,而不仅仅是单个文件config.xml。这可能就是为什么您的解决方案正在起作用

在实例化新的ClassPathXmlApplicationContext时,尝试将完整的类路径作为参数:com\sergionni\myproj\config.xml


0
投票

如果你的config xml在com.anywhere.here包中,那么试试这个:

ApplicationContext myAppContext = new ClassPathXmlApplicationContext("com/anywhere/here/config.xml");

0
投票

如果是maven项目,右键单击project-maven-update项目,这有助于我解决我的问题。


-2
投票

请做这个代码 - 它工作

AbstractApplicationContext context = new ClassPathXmlApplicationContext(“spring-config.xml”);

o / w:删除主方法类并在重新创建时重新创建它,请取消选中它工作的继承抽象方法

© www.soinside.com 2019 - 2024. All rights reserved.