In Spring Boot, you can configure and use two data sources by creating multiple DataSource beans and specifying which one to use for specific parts of your application. Here's a step-by-step guide on how to configure and use two data sources in a Spring Boot application:
Dependency Configuration: First, you need to include the required dependencies in your pom.xml or build.gradle file. For this example, we'll use H2 and PostgreSQL databases as the two data sources:
<!-- H2 Database --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <!-- PostgreSQL Database --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
Configure Data Sources: Configure the data sources in your application.properties or application.yml file. Define separate properties for each data source, specifying the JDBC URL, username, password, and other necessary settings.
# H2 DataSource spring.datasource.h2.url=jdbc:h2:mem:testdb spring.datasource.h2.username=sa spring.datasource.h2.password=password spring.datasource.h2.driverClassName=org.h2.Driver # PostgreSQL DataSource spring.datasource.postgresql.url=jdbc:postgresql://localhost:5432/mydb spring.datasource.postgresql.username=postgres spring.datasource.postgresql.password=postgres spring.datasource.postgresql.driverClassName=org.postgresql.Driver
Create DataSource Beans: Define two DataSource beans in your Spring Boot configuration class. You can use the @Configuration annotation to indicate that it's a configuration class and create two separate DataSource beans with different names.
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import javax.sql.DataSource; @Configuration public class DataSourceConfig { @Bean(name = "h2DataSource") @ConfigurationProperties(prefix = "spring.datasource.h2") public DataSource h2DataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "postgresqlDataSource") @Primary @ConfigurationProperties(prefix = "spring.datasource.postgresql") public DataSource postgresqlDataSource() { return DataSourceBuilder.create().build(); } } Use @Qualifier for Autowiring: When you want to use a specific data source in your service or repository classes, you can use the @Qualifier annotation along with the @Autowired annotation to specify which DataSource bean to inject.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import javax.sql.DataSource; @Service public class MyService { private final DataSource h2DataSource; private final DataSource postgresqlDataSource; @Autowired public MyService(@Qualifier("h2DataSource") DataSource h2DataSource, @Qualifier("postgresqlDataSource") DataSource postgresqlDataSource) { this.h2DataSource = h2DataSource; this.postgresqlDataSource = postgresqlDataSource; } // Use h2DataSource or postgresqlDataSource in your methods } Now, you have two separate data sources configured and can use them in different parts of your Spring Boot application by injecting the appropriate DataSource bean with the @Qualifier annotation.
sqldatasource mat sql-insert reportviewer sha1 baasbox google-chrome-devtools addeventlistener getch html-lists