我正在按照Spring 文档为我的 SpringBoot 应用程序使用配置文件特定的属性文件。我有 2 个属性文件src/main/resources
: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
我找到的解决方案是重命名我的属性文件,datasource-local.properties
并datasource-prod.properties
以这种方式使用 @PropertySource @PropertySource("classpath:datasource-${profile}.properties")
,当我启动 SpringBoot 应用程序时,我将其-Dprofile=local
用作 VM 选项
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句