0

I have a requirement to fetch DB username and password from Vault. So I have removed the default implementation (spring.datasource.url,spring.datasource.username,spring.datasource.password) and added the following code in DAOImpl class.

Code

  @Autowired
  private JdbcTemplate jdbcTemplate;


  @Bean
  @Primary
  public DataSource dataSource()
  {
    return DataSourceBuilder.create().username("someusername").password("somepassword")
        .url("someurl")
        .driverClassName("oracle.jdbc.driver.OracleDriver").build();
  }

It was working perfectly. But when I added a new DAOImpl class I got the following Exception. Is it necessay to add the above code snippet in all the DAOImpl classes. Is there a way to configure dataSource in single class and use it in all the DAOImpl classes

Exception

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?

Thiagarajan Ramanathan
  • 1,035
  • 5
  • 24
  • 32
  • try putting the data source into a `@Configuration` class and then just auto wire the `JdbcTemplate` into each Dao implementation class. – Kevin Oct 16 '18 at 03:46
  • Can you add your DAO and config. Look at the message in the erroe message: "Requested bean is currently in creation: Is there an unresolvable circular reference?" – Haim Raman Oct 16 '18 at 03:52
  • To retrieve things from Vault you can use the Spring Cloud Config to retrieve things from the vault and still use the default property names. Also don't add a datasource to all DAOs like that as you will create lots of datasources that way. However in short you are trying to outsmart the framework just don't do that. – M. Deinum Oct 16 '18 at 06:11
  • @Kevin: Your solution worked perfectly. Thank you. – Thiagarajan Ramanathan Oct 16 '18 at 13:33
  • @M.Deinum Acutally I am also using Spring Cloud Config to retreive things from vault. But I have stored passwords for multiple appliations in single vault mount. – Thiagarajan Ramanathan Oct 16 '18 at 13:37
  • Then just assign the mapped values to `spring.datasource` properties instead. Something like `spring.datasource.url=${someurl}` . – M. Deinum Oct 16 '18 at 13:38
  • Let me try that, if I use this approach then is it necessary to override dataSource() method? – Thiagarajan Ramanathan Oct 16 '18 at 13:44

0 Answers0