Skip to content

Commit 8d55f24

Browse files
committed
Merge pull request #44293 from quaff
* pr/44293: Polish "Include non-default DataSource candidates" Include non-default DataSource candidates Closes gh-44293
2 parents 0eeb0b6 + ceaf88c commit 8d55f24

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -113,7 +113,10 @@ private void withThreadContextClassLoader(ClassLoader classLoader, Runnable acti
113113
}
114114

115115
private List<String> getConnectionUrls(ObjectProvider<DataSource> dataSources) {
116-
return dataSources.orderedStream().map(this::getConnectionUrl).filter(Objects::nonNull).toList();
116+
return dataSources.orderedStream(ObjectProvider.UNFILTERED)
117+
.map(this::getConnectionUrl)
118+
.filter(Objects::nonNull)
119+
.toList();
117120
}
118121

119122
private String getConnectionUrl(DataSource dataSource) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java

+42-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -162,6 +162,17 @@ void allDataSourceUrlsAreLoggedWhenMultipleAvailable(CapturedOutput output) {
162162
"H2 console available at '/h2-console'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'"));
163163
}
164164

165+
@Test
166+
@ExtendWith(OutputCaptureExtension.class)
167+
void allDataSourceUrlsAreLoggedWhenNonCandidate(CapturedOutput output) {
168+
ClassLoader webAppClassLoader = new URLClassLoader(new URL[0]);
169+
this.contextRunner.withClassLoader(webAppClassLoader)
170+
.withUserConfiguration(FailingDataSourceConfiguration.class, MultiDataSourceNonCandidateConfiguration.class)
171+
.withPropertyValues("spring.h2.console.enabled=true")
172+
.run((context) -> assertThat(output).contains(
173+
"H2 console available at '/h2-console'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'"));
174+
}
175+
165176
@Test
166177
void h2ConsoleShouldNotFailIfDatabaseConnectionFails() {
167178
this.contextRunner.withUserConfiguration(FailingDataSourceConfiguration.class)
@@ -185,6 +196,20 @@ void dataSourceIsNotInitializedEarly(CapturedOutput output) {
185196
});
186197
}
187198

199+
private static DataSource mockDataSource(String url, ClassLoader classLoader) throws SQLException {
200+
DataSource dataSource = mock(DataSource.class);
201+
given(dataSource.getConnection()).will((invocation) -> {
202+
assertThat(Thread.currentThread().getContextClassLoader()).isEqualTo(classLoader);
203+
Connection connection = mock(Connection.class);
204+
DatabaseMetaData metadata = mock(DatabaseMetaData.class);
205+
given(connection.getMetaData()).willReturn(metadata);
206+
given(metadata.getURL()).willReturn(url);
207+
return connection;
208+
});
209+
210+
return dataSource;
211+
}
212+
188213
@Configuration(proxyBeanMethods = false)
189214
static class FailingDataSourceConfiguration {
190215

@@ -203,27 +228,30 @@ static class MultiDataSourceConfiguration {
203228
@Bean
204229
@Order(5)
205230
DataSource anotherDataSource() throws SQLException {
206-
return mockDataSource("anotherJdbcUrl");
231+
return mockDataSource("anotherJdbcUrl", getClass().getClassLoader());
207232
}
208233

209234
@Bean
210235
@Order(0)
211236
DataSource someDataSource() throws SQLException {
212-
return mockDataSource("someJdbcUrl");
237+
return mockDataSource("someJdbcUrl", getClass().getClassLoader());
213238
}
214239

215-
private DataSource mockDataSource(String url) throws SQLException {
216-
DataSource dataSource = mock(DataSource.class);
217-
given(dataSource.getConnection()).will((invocation) -> {
218-
assertThat(Thread.currentThread().getContextClassLoader()).isEqualTo(getClass().getClassLoader());
219-
Connection connection = mock(Connection.class);
220-
DatabaseMetaData metadata = mock(DatabaseMetaData.class);
221-
given(connection.getMetaData()).willReturn(metadata);
222-
given(metadata.getURL()).willReturn(url);
223-
return connection;
224-
});
240+
}
225241

226-
return dataSource;
242+
@Configuration(proxyBeanMethods = false)
243+
static class MultiDataSourceNonCandidateConfiguration {
244+
245+
@Bean
246+
@Order(5)
247+
DataSource anotherDataSource() throws SQLException {
248+
return mockDataSource("anotherJdbcUrl", getClass().getClassLoader());
249+
}
250+
251+
@Bean(defaultCandidate = false)
252+
@Order(0)
253+
DataSource nonDefaultDataSource() throws SQLException {
254+
return mockDataSource("someJdbcUrl", getClass().getClassLoader());
227255
}
228256

229257
}

0 commit comments

Comments
 (0)