[英]Spring Boot can't find profile-specific properties files
I am following Spring documentation to use profile specific property files for my SpringBoot app. 我正在遵循Spring文档为SpringBoot应用程序使用特定于配置文件的属性文件。 I have 2 property files under src/main/resources
: datasource.properties
for local development and datasource-prod.properties
for server datasource config. 我有以下的属性文件src/main/resources
: datasource.properties
为地方发展和datasource-prod.properties
用于服务器的数据源配置。
This is my DataSourceConfiguration.java
config class : 这是我的DataSourceConfiguration.java
配置类:
@Configuration
@PropertySource("classpath:datasource-{profile}.properties")
@Slf4j
public class DataSourceConfiguration {
@Value("${flad.datasource.driver}")
private String dataSourceDriverClassName;
@Value("${flad.datasource.url}")
private String dataSourceUrl;
@Value("${flad.datasource.username}")
private String dataSourceUsername;
@Value("${flad.datasource.password}")
private String dataSourcePassword;
@Bean
public DataSource getDataBase(){
log.info("Datasource URL = {}", dataSourceUrl);
return DataSourceBuilder
.create()
.driverClassName(dataSourceDriverClassName)
.url(dataSourceUrl)
.username(dataSourceUsername)
.password(dataSourcePassword)
.build();
}
}
When I launch my SpringBootApplication main class I get the following error whether I use -Dspring.profiles.active=prod
or not : 当我启动SpringBootApplication主类时,无论是否使用-Dspring.profiles.active=prod
我都会收到以下错误:
17:05:49.008 [restartedMain] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [fr.payet.flad.core.config.CoreConfig]; nested exception is java.io.FileNotFoundException: class path resource [datasource-{profile}.properties] cannot be opened because it does not exist
我找到的解决方案是重命名我的属性文件datasource-local.properties
和datasource-prod.properties
,以这种方式使用@PropertySource @PropertySource("classpath:datasource-${profile}.properties")
并在启动SpringBoot时使用我使用-Dprofile=local
作为VM选项的应用程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.