我正在关注Spring documentation,以便为我的SpringBoot应用程序使用特定于配置文件的属性文件。我在src/main/resources
下有2个属性文件:datasource.properties
用于本地开发,datasource-prod.properties
用于服务器数据源配置。
这是我的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();
}
}
启动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