无法在类路径上加载属性文件; [java, jboss, 行家, spring]

Can not load properties file on classpath; [java, jboss, maven, spring]

我有一个名为 app.properties 的属性文件,它已正确放入 src/main/resources/app.properties.

在 WAR 文件中它正确地位于 \WEB-INF\classes。

在我的本地环境 (windows) 的独立模式和 linux 测试服务器的独立模式下,WAR 文件启动时正确读取我的属性文件。

在另一台 linux 服务器上的 Jboss 域模式下,使用完全相同的 WAR 文件,我收到错误文件未找到 app.properties。确实有。

除了域模式是两台服务器之间的区别外,第一个测试服务器 jboss 安装在 root 下,运行 作为 root 安装。另一台服务器是 运行 作为具有读取和执行访问权限的用户。

我已经用 print 语句彻底调试了代码,我 99% 确定这不是代码问题,jboss 域模式中的任何想法都可能导致无法读取的问题类路径上的属性文件?

提前致谢。

代码的相关部分

MutablePropertySources sources = new MutablePropertySources();
sources.addLast(getEncryptablePropertiesSource(new ClassPathResource("app.properties")));

部分方法

private EncryptablePropertiesPropertySource getEncryptablePropertiesSource(Resource propsResource) throws IOException{
    //don't use file system resource because property files may be in a jar
    System.out.println(">>>> in getEncryptablePropertiesSource filename is :");
    System.out.println(propsResource.getFilename());
    System.out.print(">>>> URL is: ");      
    System.out.println(propsResource.getURL());

最后一个系统输出打印语句在第二个测试环境中抛出错误,在任何其他环境中都不会导致问题。

如果您的 ClassPathResource 是来自 Spring 的 class:

public class ClassPathResource extends AbstractFileResolvingResource

Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources.

Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL.

因此我认为您不能在您的情况下使用它。 您是否尝试过使用以下方法之一?