Проблема соединения с БД через DriverManagerDataSource и файл properties

Ниже указан рабочий метод, через который соединение устанавливается корректно:

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("org.postgresql.Driver");
    dataSource.setUrl("jdbc:postgresql://localhost:5433/database");
    dataSource.setUsername("postgres");
    dataSource.setPassword("password");
    return dataSource;
}

Когда я использую для соединения файл dbConnection.properties вылетает ошибка -

Request processing failed; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection: 'url' not set

Метод подключения:

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("src/main/resources/dbConnection.properties"));

        dataSource.setDriverClassName(properties.getProperty("driver"));
        dataSource.setUrl(properties.getProperty("url"));
        dataSource.setUsername(properties.getProperty("username"));
        dataSource.setPassword(properties.getProperty("password"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return dataSource;
}

Устанавливаемые значения одинаковые. В чем может быть ошибка?


Ответы (0 шт):