java—有没有一种方法可以让SpringBoot应用程序能够在资源中看到config文件夹,并在需要时获取其配置?

jogvjijk  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(243)

我有一个配置文件夹的xml文件,我的 Spring 启动应用程序需要,它位于同一级别的src文件夹。。。现在我要在同一级别的application.properties的resources文件夹中找到它。。有什么办法可以做到吗?

j0pj023g

j0pj023g1#

有很多方法可以读取文件。属性:
您可以使用以下纯java进行阅读:

Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));

你可以用 @PropertySource Spring里的注解 @Configuration 班级:

@Configuration
@PropertySource("db.properties")
public class ApplicationConfig {

    // more config ...
}
//Or if you your config outside of your classpath, you can use the file: prefix:
@PropertySource("file:/path/to/application.properties")

使用多个文件:

@PropertySources({
    @PropertySource("classpath:foo.properties"),
    @PropertySource("classpath:bar.properties")
})
public class PropertiesWithJavaConfig {
    //...
}

等等。。。

相关问题