Spring:在类路径中找不到配置 xml
对于我的简单 Maven 项目,这不起作用:
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
config.xml
驻留在同一类级别
实际上如何将 config.xml 添加到类路径? 注意:我的项目是一个lib,如果我在其他Web项目中执行相同的配置 web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config.xml</param-value>
</context-param>
它工作正常
在这里我不需要 web.xml,只需要正确的类路径。
For my simple maven project this doesn't work:
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
config.xml
is resided at the same class level
How,actually,add config.xml to classpath?
note: my project is a lib,if I do the same in other web project with configuration in
web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config.xml</param-value>
</context-param>
that it works OK
Here I needn't web.xml, just correct classpath.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您输入
classpath*:config.xml
时,classpath*
是一个通配符,表示您要加载与config.xml
匹配的每个文件在整个类路径上,而不仅仅是单个文件 config.xml。这可能就是您的解决方案无法正常工作的原因。实例化新的
ClassPathXmlApplicationContext
时,尝试提供完整的类路径作为参数:com\sergionni\myproj\config.xml
。When you enter
classpath*:config.xml
, theclasspath*
is a wild card indicates that you want to load every file matchingconfig.xml
on the entire classpath, not just the single fileconfig.xml
. This may be why your solution is working otherwise.When instantiating a new
ClassPathXmlApplicationContext
, try giving the full classpath as an argument:com\sergionni\myproj\config.xml
.如果您的配置 xml 位于包 com.anywhere.here 中,请尝试以下操作:
If your config xml is in package com.anywhere.here then try this:
如果是 Maven 项目,右键单击project-maven-update 项目,这帮助我解决了我的问题。
In case of maven project, right click on project-maven-update project, this helped me solve my issue.
请执行此代码 - 它有效
AbstractApplicationContext context= new ClassPathXmlApplicationContext("spring-config.xml");
o/w:删除主方法类并在重新创建时重新创建它,请取消选中“继承的抽象方法”
Please do This code - it worked
AbstractApplicationContext context= new ClassPathXmlApplicationContext("spring-config.xml");
o/w: Delete main method class and recreate it while recreating please uncheck Inherited abstract method its worked