I have a endpoint which exports the data from the database and I have another endpoint which imports the data . I want to export the data from the one database and import to other but that should be done at runtime , somewhat like this : To export
http://localhost:8080/export?source=oracle
To import
http://localhost:8080/import?source=mariadb
This is how I am reading the property file currently:
@Component
public class ExportDBConfiguration extends DBConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(ExportDBConfiguration.class);
@Value("${export.database.url}")
private String dbUrl;
@Value("${export.database.user}")
private String dbUser;
@Value("${export.database.password}")
private String dbPassword;
public Properties getDatabaseProperties() {
LOG.info("Setting export database specific properties.");
return getDatabaseProperties(dbUser, dbPassword);
}
public String getDBUrl() {
String absoluteUrl = getDBUrl(dbUrl);
LOG.info("Absolute db url is :: {} ", absoluteUrl);
return absoluteUrl;
}
}
To achieve this I thought of creating the separate properties file and then read from them based on the parameter source of the endpoint , but this approach is not working . Any idea how can it be done or a better approach to achieve this loose coupling ??
You should use multiple
DataSourcesin your Spring Boot App.There are several Articles, Tutorials and SO answers that explain this topic.
For starters, you add the config of your
datasourcesin theapplication.propertiesfile:Then create config classes for all your
DataSources:Then another Config class for your second DB:
A lot of the part above is explained in this, this, and this article.
And then depending on what database name is in your query params, you use the specific Repos and Models. You can of course add more than 2
DataSources.