有没有办法让 spring 启动应用程序能够在资源中看到配置文件夹并在需要时获取其配置?
Is there is a way to make a spring boot app to be able to see a config folder in Resources and get its config whenever it needs?
我有一个 config 文件夹,其中包含 xml 个文件,我的 spring 启动应用程序需要它,它位于在 src 文件夹的同一级别上...现在我将它放在 Resources 文件夹 中 [ 的同一级别上=23=]..有什么方法可以做到吗?
读取文件 .properties 的方法有很多:
您可以像这样使用普通 java 阅读:
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
您可以在 Spring @Configuration
class:
中使用 @PropertySource
注释
@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 {
//...
}
等等...
我有一个 config 文件夹,其中包含 xml 个文件,我的 spring 启动应用程序需要它,它位于在 src 文件夹的同一级别上...现在我将它放在 Resources 文件夹 中 [ 的同一级别上=23=]..有什么方法可以做到吗?
读取文件 .properties 的方法有很多:
您可以像这样使用普通 java 阅读:
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
您可以在 Spring @Configuration
class:
@PropertySource
注释
@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 {
//...
}
等等...