|
24 | 24 | import org.junit.Before;
|
25 | 25 | import org.junit.Test;
|
26 | 26 | import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
| 27 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 28 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
27 | 29 | import org.springframework.boot.test.EnvironmentTestUtils;
|
28 | 30 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
| 31 | +import org.springframework.context.annotation.Bean; |
| 32 | +import org.springframework.context.annotation.Configuration; |
| 33 | +import org.springframework.context.annotation.Primary; |
29 | 34 | import org.springframework.jdbc.core.JdbcOperations;
|
30 | 35 | import org.springframework.jdbc.core.JdbcTemplate;
|
31 | 36 | import org.springframework.util.ClassUtils;
|
@@ -67,6 +72,19 @@ public void testDefaultDataSourceDoesNotExists() throws Exception {
|
67 | 72 | assertEquals(0, this.context.getBeanNamesForType(DataSource.class).length);
|
68 | 73 | }
|
69 | 74 |
|
| 75 | + @Test |
| 76 | + public void testTwoDataSources() throws Exception { |
| 77 | + EnvironmentTestUtils.addEnvironment(this.context, |
| 78 | + "datasource.one.url=jdbc:hsqldb:mem:/one", |
| 79 | + "datasource.one.driverClassName=org.hsqldb.Driver", |
| 80 | + "datasource.two.url=jdbc:hsqldb:mem:/two", |
| 81 | + "datasource.two.driverClassName=org.hsqldb.Driver"); |
| 82 | + this.context.register(TwoDataSources.class, DataSourceInitializer.class, |
| 83 | + PropertyPlaceholderAutoConfiguration.class, DataSourceProperties.class); |
| 84 | + this.context.refresh(); |
| 85 | + assertEquals(2, this.context.getBeanNamesForType(DataSource.class).length); |
| 86 | + } |
| 87 | + |
70 | 88 | @Test
|
71 | 89 | public void testDataSourceInitialized() throws Exception {
|
72 | 90 | EnvironmentTestUtils.addEnvironment(this.context,
|
@@ -125,4 +143,23 @@ public void testDataSourceInitializedWithMultipleScripts() throws Exception {
|
125 | 143 | template.queryForObject("SELECT COUNT(*) from SPAM", Integer.class));
|
126 | 144 | }
|
127 | 145 |
|
| 146 | + @Configuration |
| 147 | + @EnableConfigurationProperties |
| 148 | + protected static class TwoDataSources { |
| 149 | + |
| 150 | + @Bean |
| 151 | + @Primary |
| 152 | + @ConfigurationProperties(prefix = "datasource.one") |
| 153 | + public DataSource oneDataSource() { |
| 154 | + return DataSourceBuilder.create().build(); |
| 155 | + } |
| 156 | + |
| 157 | + @Bean |
| 158 | + @ConfigurationProperties(prefix = "datasource.two") |
| 159 | + public DataSource twoDataSource() { |
| 160 | + return DataSourceBuilder.create().build(); |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | + |
128 | 165 | }
|
0 commit comments