Ghassen
Ghassen

Reputation: 781

Spring Boot can't find profile-specific properties files

I am following Spring documentation to use profile specific property files for my SpringBoot app. I have 2 property files under src/main/resources : datasource.properties for local development and datasource-prod.properties for server datasource config.

This is my DataSourceConfiguration.java config class :

@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 :

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

Upvotes: 0

Views: 2124

Answers (1)

Ghassen
Ghassen

Reputation: 781

The solution that I found is to rename my property files datasource-local.properties and datasource-prod.properties, use @PropertySource this way @PropertySource("classpath:datasource-${profile}.properties") and when I launch my SpringBoot app I use -Dprofile=local as VM options

Upvotes: 1

Related Questions