[自定义文件夹中的Spring Boot加载属性文件

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

我需要从src / main / resources / config文件夹加载属性文件。加载部分编写在一个普通的依赖项目中,在该项目中我们没有任何控制权。我们只是通过依赖项传递表示的文件名。从属jar中的代码段如下所示,即标准资源加载。

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propertyFileName);

Spring总是会直接在recources文件夹下寻找资源,在这种情况下,它无法像在自定义文件夹中一样加载文件,而不能在类路径下加载它。

我们如何将自定义文件夹明确设置为其他类路径文件夹?

使用Maven,我们可以执行以下类似操作,效果很好。在Spring Boot中,有没有任何OOTB方式可以通过注释实现这一目标?

    `<resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>src/main/resources/config</directory>
        </resource>
    </resources>`

更新

    `// This works if config.properties is directly under resource folder
    // What if config.properties is under resources/config folder.
    // Dont say to pass argument as /config/config.properties, there are some other limitations.
    // So in that case with the same approach, config should come under classpath, so that the below
    // method will work always when the property name is passed.
    // As mentioned earlier, we can use maven resource settings to achieve this.
    // The question here is, is there any way to explicitely advise spring to load property from this folder.
    // I have seen something like loader.path config, not sure that helps!
    InputStream stream = SpringBootStarter.class.getResourceAsStream("/config.properties");`
spring-boot maven
1个回答
0
投票

在回答之前,当您说:Spring will always look for recources under recources folder directly, in this case its unable to load the file as its in the custom folder and its not under classpath.时,这是不正确的。

Spring可以在系统上的任何地方显示。这是如何使用Spring和Spring boot加载不同的属性文件的方法:

  • [@PropertySource("classpath:config/common.properties") =>将在类路径的下面,找到位于类路径根目录下config文件夹下的文件。
  • [@PropertySource("file:/config/common.properties") =>将在文件系统的根目录/config/common.properties下查找文件common.properties。

现在,存在“什么是类路径”的问题,似乎值得更多解释。

类路径适用于JVM,而文件系统适用于您的OS。当您执行一些Java代码(例如.jar文件)时,JVM将存储您指定的所有文件。您可以在执行java -classpath /a/shared/folder,/a/dependency/app.jar,myApp.jar MainClass时指定文件。 (请参见其他一些方法:https://javarevisited.blogspot.com/2012/10/5-ways-to-add-multiple-jar-to-classpath-java.html)。通常,对于开发人员(在使用Spring之前)会发生以下情况:

  1. 我们开发应用程序,并使用maven来管理依赖项
  2. 我们使用IDE执行我们的应用,一切正常,生活很美好
  3. 我们已经准备好投入生产(投入生产)。我们生成了著名的myApp.jar,然后尝试执行应用程序java -jar myApp.jar和...。您在使用Java时遇到了问题(我假设您在清单中设置了主类...),并且得到了类似Caused by: java.lang.ClassNotFoundException: my.dependency.OtherClass ...
  4. 的信息
  5. 最后,您意识到生活很艰难,现在还没有准备好生活。您需要具备可以轻松执行的功能。

为避免类路径问题,一种可能的解决方案是将所有内容放入JAR(在spring-boot中称为FAT jar,然后使用java -jar myApp.jar,它可以正常工作。

默认情况下,当您生成Maven项目时,会自动包含一些文件夹,例如:

  • [src/main/java =>您的Java文件和软件包]
  • [src/main/resources =>您的配置文件(如.properties)
  • src/test/java =>您的Java测试文件
  • src/test/resources =>可用于测试的资源

当您生成jar时(添加到maven项目中的每个配置或多或少都可以,但是可以说,这没关系),编译器将src/main/javasrc/main/resources下的所有文件夹和文件都放入并放置它们在罐子的根部(不要犹豫,看看您的jar文件内部。这只是一个Zip,您可以打开它,浏览它并亲自查看)。

话虽如此,当您说How do we explicitly set the custom folder as additional classpath folder?时,您谈论的是src/main/resources下的自定义文件夹,然后在生成Jar时,该自定义文件夹将位于jar中,并因此位于您的类路径中。

如果您仍然遇到麻烦,此操作将为您提供帮助:

  • 解压缩jar文件并检查其中的内容。如果其中没有看到任何config/文件夹,则可能是您的Jar代是错误的
  • 尝试使用@PropertySource(...)在类路径和文件系统中加载属性文件,以查看其工作原理和所获得的成果
  • 也来看看这个:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>

  • 不要犹豫,将越来越多的旧代码迁移到Spring-boot,对您来说会容易得多。
© www.soinside.com 2019 - 2024. All rights reserved.