There are plenty of resources explaining how to configure data source in Spring Boot using various options and various ways to retrieve the configuration details of the connections (from external file, from, hard-coded, etc).
On the other hand, if you already have a data source configured and at runtime you want to change the details of this connection (perhaps hostname of database changes) without restarting the application, the information is a bit scarcer.

The use-case I am talking about is a situation where the user is allowed to change all or part of the connection details to the database (hostname for example).
The user has a Settings page where he can edit these details. This settings are kept in an external file, not in
In this case, for the subsequent requests to that database, the new parameters need to be used.
The default behavior of Spring is to create Beans as Singleton, including the DataSource that you configure and thus, the bean won’t be recreated if you change the settings (it will still use the old parameters).

Here I present one of many approaches as to how this can be achieved.

Creating the data source

There are multiple ways to create a data source to use in Spring Boot and plenty of resources for this.
Here, I show how to create a simple JdbcTemplate based Repository configured in a separate package (will not use database properties defined in

Here’s the configuration snippet for the data source:

public class CustomDataSourceConfiguration {

public DataSource customDataSource() {
DataSourceBuilder dsBuilder = DataSourceBuilder.create();
CustomDatabaseSettings dbSettings = <….>//Here obtain the settings from whereever you need

public JdbcTemplate customJdbcTemplate() {
return new JdbcTemplate(customDataSource());


Here, using a class annotated as Configuration class, I define the Beans needed for the DataSource and for the JdbcTemplate.
The @Lazy annotation is important in this context since I read the external file which holds the database configuration from Java code and this happens after Spring Container initializes all beans.
By default, Beans are loaded Eagerly, meaning that when Spring scans for them and meets them, it will instantiate them. Lazy will wait with the initialization until the first request for that bean is made.
See the doc for @Lazy –

The Scope of the bean is set to ‚Prototype‘. By default the scope is set to Singleton, but as I said in the beginning of the article, we need a way to reinitialize the data source with new configuration details and the easiest way is using this scope. This scope will call the initialization each time the bean is requested.
As you can see from the code, this suits our case very well since calling again the method to initialize will also get the new configured database connection details.
There are some caveats to using Prototype.
More info about the scopes can be found in the docs:

Here’s the code the Repository as well:

@ComponentScan(basePackages = „com.cli.jdbc.datasource“)
public class CustomRepository{

private JdbcTemplate jdbcTemplate;

//Implement methods here

So we’ve configured the DataSource by reading from our custom Configuration file which changes dynamically at runtime.
Now, when the Settings are changed, we need a way to reinitialize the data source.
Conceptually this is done by simply retrieving the DataSource bean from the Spring Context. Because we used Prototype scope, this will cause the logic in the above defined method to be called again which in turn will take the newest changes for our database. After this step, we need to retrieve the jdbcTemplate from the context as well and set this new DataSource to it.

The code looks like this:

public void refreshCustomJdbc() {
DataSource ds = (DataSource) getSpringContext().getBean(„customDataSource“);
JdbcTemplate customJdbcTemplate = (JdbcTemplate) getSpringContext().getBean(„customJdbcTemplate“);


Der virtual7 Podcast!

Frei nach dem Motto: „Geht ins Ohr – bleibt im Kopf“ startet die virtual7 GmbH ihren Podcast „Expertengeflüster“.

Getrieben durch die Mission, die digitale Zukunft Deutschlands zu gestalten, werden verschiedene Themen rund um KI, Analytics, DevOps, Fachverfahrensentwicklung und natürlich auch die Unternehmensentwicklung von virtual7 behandelt.

Gelangen Sie hier zu den ersten Folgen!