Search code examples
spring-bootspring-cloudspring-cloud-config

@Refreshscope with Datasource configuration


I am having a datasource configuration class in a Spring boot app. Snippet below

My configuration is fetched from Spring cloud config server. When I change my DB hostname and refresh using /refresh endpoint, the app is NOT using new DB host. ANy idea why ?

@Configuration
@RefreshScope
public classe DBConfig
{
  @Resource
    private Environment env;

   private DataSource ehubDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("datasource.driverClassName"));
        dataSource
                .setUrl(env.getProperty("datasource.url"));
        dataSource.setUsername(env.getProperty("datasource.username"));
        dataSource.setPassword(env.getProperty("datasource.password"));
        return dataSource;
    }

}

Solution

  • As per docs,@RefreshScope will technically work on @Configuration, provided anything that depends on those beans cannot rely on them being updated when a refresh is initiated, unless it is itself in @RefreshScope

    So could you please check your "Environment.java", You may forget to specify @RefreshScope in Environment.java. Please share your Environment.java if it is not working.