|
| 1 | +package example.springdata.jdbc.howto.multipledatasources.configuration; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 4 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 5 | +import org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration; |
| 6 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.context.annotation.Lazy; |
| 10 | +import org.springframework.data.jdbc.core.convert.DefaultJdbcTypeFactory; |
| 11 | +import org.springframework.data.jdbc.core.convert.JdbcConverter; |
| 12 | +import org.springframework.data.jdbc.core.convert.JdbcCustomConversions; |
| 13 | +import org.springframework.data.jdbc.core.convert.MappingJdbcConverter; |
| 14 | +import org.springframework.data.jdbc.core.convert.RelationResolver; |
| 15 | +import org.springframework.data.jdbc.core.dialect.DialectResolver; |
| 16 | +import org.springframework.data.jdbc.core.dialect.JdbcDialect; |
| 17 | +import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; |
| 18 | +import org.springframework.data.relational.core.mapping.DefaultNamingStrategy; |
| 19 | +import org.springframework.data.relational.core.mapping.NamingStrategy; |
| 20 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
| 21 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
| 22 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 23 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 24 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 25 | +import org.springframework.transaction.PlatformTransactionManager; |
| 26 | + |
| 27 | +import javax.sql.DataSource; |
| 28 | +import java.util.Optional; |
| 29 | + |
| 30 | +@Configuration |
| 31 | +public class DataSourceConfiguration { |
| 32 | + |
| 33 | + @Configuration |
| 34 | + static class Ds1Configuration { |
| 35 | + |
| 36 | + @Bean("ds1") |
| 37 | + DataSource dataSource1() { |
| 38 | + |
| 39 | + var builder = new EmbeddedDatabaseBuilder(); |
| 40 | + |
| 41 | + return builder |
| 42 | + .setType( EmbeddedDatabaseType.H2 ) |
| 43 | + .generateUniqueName( true ) |
| 44 | + .addScript( "classpath:/ds1-schema.sql" ) |
| 45 | + .addScript( "classpath:/ds1-data.sql" ) |
| 46 | + .build(); |
| 47 | + } |
| 48 | + |
| 49 | + @Bean("jdbcOperations1" ) |
| 50 | + NamedParameterJdbcOperations jdbcOperations1() { |
| 51 | + |
| 52 | + return new NamedParameterJdbcTemplate( dataSource1() ); |
| 53 | + } |
| 54 | + |
| 55 | + @Bean( "transactionManager1" ) |
| 56 | + PlatformTransactionManager transactionManager1() { |
| 57 | + |
| 58 | + return new DataSourceTransactionManager( dataSource1() ); |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + @Configuration |
| 64 | + @EnableAutoConfiguration(exclude = { |
| 65 | + DataSourceAutoConfiguration.class, |
| 66 | + JdbcRepositoriesAutoConfiguration.class |
| 67 | + }) |
| 68 | + static class Ds2Configuration { |
| 69 | + |
| 70 | + @Bean( "ds2" ) |
| 71 | + DataSource dataSource2() { |
| 72 | + |
| 73 | + var builder = new EmbeddedDatabaseBuilder(); |
| 74 | + |
| 75 | + return builder |
| 76 | + .setType( EmbeddedDatabaseType.H2 ) |
| 77 | + .generateUniqueName( true ) |
| 78 | + .addScript( "classpath:/ds2-schema.sql" ) |
| 79 | + .addScript( "classpath:/ds2-data.sql" ) |
| 80 | + .build(); |
| 81 | + } |
| 82 | + |
| 83 | + @Bean( "jdbcOperations2" ) |
| 84 | + NamedParameterJdbcOperations jdbcOperations1() { |
| 85 | + |
| 86 | + return new NamedParameterJdbcTemplate( dataSource2() ); |
| 87 | + } |
| 88 | + |
| 89 | + @Bean( "transactionManager2" ) |
| 90 | + PlatformTransactionManager transactionManager2() { |
| 91 | + |
| 92 | + return new DataSourceTransactionManager( dataSource2() ); |
| 93 | + } |
| 94 | + |
| 95 | + @Bean |
| 96 | + JdbcDialect jdbcDialect2( @Qualifier( "jdbcOperations2" ) NamedParameterJdbcOperations jdbcOperations ) { |
| 97 | + |
| 98 | + return DialectResolver.getDialect( jdbcOperations.getJdbcOperations() ); |
| 99 | + } |
| 100 | + |
| 101 | + @Bean |
| 102 | + JdbcCustomConversions customConversions2() { |
| 103 | + |
| 104 | + return new JdbcCustomConversions(); |
| 105 | + } |
| 106 | + |
| 107 | + @Bean |
| 108 | + JdbcMappingContext jdbcMappingContext2( Optional<NamingStrategy> namingStrategy, JdbcCustomConversions customConversions2 ) { |
| 109 | + |
| 110 | + var mappingContext = new JdbcMappingContext( namingStrategy.orElse( DefaultNamingStrategy.INSTANCE ) ); |
| 111 | + mappingContext.setSimpleTypeHolder( customConversions2.getSimpleTypeHolder() ); |
| 112 | + |
| 113 | + return mappingContext; |
| 114 | + } |
| 115 | + |
| 116 | + @Bean |
| 117 | + JdbcConverter jdbcConverter2( |
| 118 | + JdbcMappingContext jdbcMappingContext2, |
| 119 | + @Qualifier( "jdbcOperations2" ) NamedParameterJdbcOperations jdbcOperations2, |
| 120 | + @Lazy RelationResolver relationResolver, |
| 121 | + JdbcCustomConversions customConversions2 |
| 122 | + ) { |
| 123 | + |
| 124 | + var jdbcTypeFactory = new DefaultJdbcTypeFactory( jdbcOperations2.getJdbcOperations() ); |
| 125 | + |
| 126 | + return new MappingJdbcConverter( jdbcMappingContext2, relationResolver, customConversions2, jdbcTypeFactory ); |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments