Spring DataSource password in separate properties file causes “Failed to determine a suitable driver class”

Tags: ,



I have the following Spring configuration

myprefix.systems[0].name=Some System
myprefix.systems[0].datasource.driverclassname=oracle.jdbc.OracleDriver
myprefix.systems[0].datasource.url=jdbc:oracle:thin:@foo:1521/bar
myprefix.systems[0].datasource.username=username
myprefix.systems[0].datasource.password=password

Which is configured into the following class (annotations are lombok and Spring).

@Configuration
@ConfigurationProperties("myprefix")
public class SystemConfig {
    @Getter
    @Setter
    public static class ConfiguredSystem {
        private final DataSourceProperties datasource = new DataSourceProperties();
        private String name;

        public JdbcTemplate getTemplate() {
            return new JdbcTemplate(datasource.initializeDataSourceBuilder().build());
        }
    }

    @Getter
    private final List<ConfiguredSystem> systems = new ArrayList<>();
    
    @Bean
    public List<ConfiguredSystem> allSystems() {
        return Collections.unmodifiableList(tradingSystems);
    }
}

This works just fine when all the properties are in one application.properties file. The application starts up properly.

I am trying to move the password line into it’s own application-secret.properties file, and keep the other properties in the main application.properties file.

myprefix.systems[0].datasource.password=password

I run with

-Dspring.config.location="C:/my/app/dir/conf/"
-Dspring.profiles.active=secret

However, when I do this I get the following error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (the profiles secret are currently active).
 | o.s.b.d.LoggingFailureAnalysisReporter [main]

After putting a breakpoint in getTemplate, it seems the DataSourceProperties only contains the password and none of the other properties. I presume Spring cannot do list comprehension (for lack of a better term), myprefix.systems[0]... across different files?

Answer

This won’t answer why the initial solution (merging myprefix.systems[0].datasource.password) did not work, but you may solve your problem by:

  • Creating a db.password property in the application-secret.properties
  • Use ${db.password} in application.properties.


Source: stackoverflow