0
I am developing an app with Spring Boot and I have a configuration class that generates a @Bean Datasource.
I would like to generate alternative connection levels of connection to the database, as if the environment variables are incorrect, use application.properties data, if this is also, use configuration class, regardless of this order, would like a similar result, it is possible to configure this way, have some doc I can follow ?
I believe I have a more dynamic and reactive environment for connecting in this way.
Datasource Bean Class
package foo.bar.configuration;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
public class DataBaseConfiguration {
private final static String DATABASE_CONNECTION_ROOT_USERNAME = "foo";
private final static String DATABASE_CONNECTION_ROOT_PASSWORD = "bar";
private final static String DATABASE_CONNECTION_URL = "jdbc:mysql://localhost:3306/foobar";
@Bean
@Primary
public DataSource dataSource() {
final DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName(DatabaseDriver.MYSQL.getDriverClassName());
driverManagerDataSource.setUrl(DATABASE_CONNECTION_URL);
driverManagerDataSource.setUsername(DATABASE_CONNECTION_ROOT_USERNAME);
driverManagerDataSource.setPassword(DATABASE_CONNECTION_ROOT_PASSWORD);
return driverManagerDataSource;
}
}
I don’t quite understand your problem: you will have configuration information in several places, several Property source, for example? Take a look at import Configuration, Property sources, etc. Moreover, it seems to have nothing to do with reactivity, since JDBC is not reactive, being by default blocking -, if this is your problem, look for clients SQL that are.
– Bruno César
Thanks for the feedback. Yes I believe I was wrong to say about reactivity, I confess that I am still learning a little, I thought that the expression, would be more appropriate to the questioning, however would have, some tip for reactive sql clients ?
– Abraão Borges Neves