gpt4 book ai didi

java - 将 spring XML 文件添加到类路径(Windows 命令行)

转载 作者:行者123 更新时间:2023-11-30 09:23:49 25 4
gpt4 key购买 nike

我正在尝试通过使用 Spring 和 spring xml 配置文件的 cmd 行运行 jar 文件。

cmd 行调用类似于:

java -cp lib/MyJar.jar my.package.MyClass

我得到的错误是:

Caused by: java.io.FileNotFoundException: class path resource 
[myPath/mySpringCfg.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)

我的 list 类路径类似于:

Class-Path: 3rdPartyJar1.jar 3rdPartyJar2.jar ./myPath/

加载文件的调用等同于:

context = new ClassPathXmlApplicationContext("myPath/mySpringCfg.xml");

有没有办法在类路径中正确提取 XML 文件,以便 Spring 能够按预期工作?类路径文档似乎只讨论归档文件和文件夹。

谢谢!

更新

当我切换到 FileSystemXmlApplicationContext 时,它似乎运行良好。我想不能从命令行使用 ClassPathXmlApplicationContext

最佳答案

您对 XML 的引用是 myPath/mySpringCfg.xml - 这意味着 myPath 必须在类路径中。

将您的 list 更改为:

Class-Path: 3rdPartyJar1.jar 3rdPartyJar2.jar ./

这样 myPath 将成为类路径的一部分,而不仅仅是它的内容。

注意:应用程序配置 XML 是应用程序代码的一部分,不要将其误认为是配置。

如果您需要配置 - 将其放在属性文件中并在您的 XML 配置文件中使用占位符。

更新:

我认为你的问题的根本原因在代码中(虽然我没有测试它) - 试试这个:

context = new ClassPathXmlApplicationContext("/myPath/mySpringCfg.xml");

区别在于'myPath'之前的'/'

关于java - 将 spring XML 文件添加到类路径(Windows 命令行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15908503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com