ClassPathXmlApplicationContext 读取 JAR 外部的文件

发布于 2024-12-29 03:53:15 字数 632 浏览 0 评论 0原文

我的代码使用以下代码实例化一个 spring 实例:

String filePath = "applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(filePath);

如果我将名为“applicationContext.xml”的文件放在 WEB-INF/lib 文件夹中 jar 的根目录中,则此方法有效。但是,我希望在 WEB-INF/classes 文件夹中有一个名为“applicationContext.xml”的文件。我已尝试使用以下 filePath 值,所有这些值都会生成 FileNotFoundException:

applicationContext.xml
/webappName/WEB-INF/classes/applicationContext.xml
/WEB-INF/classes/applicationContext.xml
classes/applicationContext.xml

这些都不起作用。使用 ClassPathXmlApplicationContext 从 WEB-INF/classes 文件夹中读取 xml 文件的正确语法是什么?

My code instantiates a spring instance using the following code:

String filePath = "applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(filePath);

This works if I put a file named "applicationContext.xml" in the root of a jar in my WEB-INF/lib folder. However, I would prefer to have a file named "applicationContext.xml" in my WEB-INF/classes folder. I have tried the following values for filePath and all of them generate a FileNotFoundException:

applicationContext.xml
/webappName/WEB-INF/classes/applicationContext.xml
/WEB-INF/classes/applicationContext.xml
classes/applicationContext.xml

None of these has worked. What is the correct syntax to use with ClassPathXmlApplicationContext to read an xml file out of the WEB-INF/classes folder?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妥活 2025-01-05 03:53:15

如下所述,您应该尝试 /applicationContext.xml

As commented below, you should try /applicationContext.xml

绿萝 2025-01-05 03:53:15

使用 FileSystemXmlApplicationContext

Use FileSystemXmlApplicationContext

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文