弹簧从不同的 Maven 模块读取属性文件


demo-parent:
--web
--src
--main
--java
--Application.java
--resources
--application.yml
--application-mysql.yml
--service
--common
--src
--main
--java
--Config.java
--resources
--core-application.yml

core-application有一些spring properties,比如spring.kafka.properties.bootstrap.servers.如何在我的Web 模块中获取它们?

我现在FileNotFoundException: class path resource [core-application.yml] cannot be opened because it does not exist有一个错误。

根据 Java 8 约定,@PropertySource注解是可重复的。因此,如果我们使用的是 Java 8 或更高版本,我们可以使用此注释来定义多个属性位置:

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

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

请参阅本文,https://www.baeldung.com/properties-with-spring

最新更新