繁体   English   中英

Spring Boot找不到特定于配置文件的属性文件

[英]Spring Boot can't find profile-specific properties files

我正在遵循Spring文档为SpringBoot应用程序使用特定于配置文件的属性文件。 我有以下的属性文件src/main/resourcesdatasource.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.propertiesdatasource-prod.properties ,以这种方式使用@PropertySource @PropertySource("classpath:datasource-${profile}.properties")并在启动SpringBoot时使用我使用-Dprofile=local作为VM选项的应用程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM