Skip to content

Commit 48adfe6

Browse files
committed
Log H2 console path and JDBC URL on startup
Closes gh-17063
1 parent d126984 commit 48adfe6

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,23 @@
1616

1717
package org.springframework.boot.autoconfigure.h2;
1818

19+
import java.sql.Connection;
20+
import java.sql.SQLException;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.apache.commons.logging.Log;
25+
import org.apache.commons.logging.LogFactory;
1926
import org.h2.server.web.WebServlet;
2027

28+
import org.springframework.beans.factory.ObjectProvider;
29+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2130
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2231
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2332
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2433
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2534
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
35+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
2636
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2737
import org.springframework.boot.web.servlet.ServletRegistrationBean;
2838
import org.springframework.context.annotation.Bean;
@@ -40,11 +50,15 @@
4050
@ConditionalOnWebApplication(type = Type.SERVLET)
4151
@ConditionalOnClass(WebServlet.class)
4252
@ConditionalOnProperty(prefix = "spring.h2.console", name = "enabled", havingValue = "true", matchIfMissing = false)
53+
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
4354
@EnableConfigurationProperties(H2ConsoleProperties.class)
4455
public class H2ConsoleAutoConfiguration {
4556

57+
private static final Log logger = LogFactory.getLog(H2ConsoleAutoConfiguration.class);
58+
4659
@Bean
47-
public ServletRegistrationBean<WebServlet> h2Console(H2ConsoleProperties properties) {
60+
public ServletRegistrationBean<WebServlet> h2Console(H2ConsoleProperties properties,
61+
ObjectProvider<DataSource> dataSource) {
4862
String path = properties.getPath();
4963
String urlMapping = path + (path.endsWith("/") ? "*" : "/*");
5064
ServletRegistrationBean<WebServlet> registration = new ServletRegistrationBean<>(new WebServlet(), urlMapping);
@@ -55,6 +69,15 @@ public ServletRegistrationBean<WebServlet> h2Console(H2ConsoleProperties propert
5569
if (settings.isWebAllowOthers()) {
5670
registration.addInitParameter("webAllowOthers", "");
5771
}
72+
dataSource.ifAvailable((available) -> {
73+
try (Connection connection = available.getConnection()) {
74+
logger.info("H2 console available at '" + path + "'. Database available at '"
75+
+ connection.getMetaData().getURL() + "'");
76+
}
77+
catch (SQLException ex) {
78+
// Continue
79+
}
80+
});
5881
return registration;
5982
}
6083

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@
1616

1717
package org.springframework.boot.autoconfigure.h2;
1818

19+
import java.sql.Connection;
20+
import java.sql.SQLException;
21+
22+
import javax.sql.DataSource;
23+
1924
import org.junit.jupiter.api.AfterEach;
2025
import org.junit.jupiter.api.BeforeEach;
2126
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.extension.ExtendWith;
2228

29+
import org.springframework.beans.BeansException;
2330
import org.springframework.beans.factory.BeanCreationException;
31+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
32+
import org.springframework.boot.test.system.CapturedOutput;
33+
import org.springframework.boot.test.system.OutputCaptureExtension;
2434
import org.springframework.boot.test.util.TestPropertyValues;
2535
import org.springframework.boot.web.servlet.ServletRegistrationBean;
2636
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
@@ -113,4 +123,15 @@ void customInitParameters() {
113123
assertThat(registrationBean.getInitParameters()).containsEntry("webAllowOthers", "");
114124
}
115125

126+
@Test
127+
@ExtendWith(OutputCaptureExtension.class)
128+
void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput capturedOutput) throws BeansException, SQLException {
129+
this.context.register(DataSourceAutoConfiguration.class, H2ConsoleAutoConfiguration.class);
130+
TestPropertyValues.of("spring.h2.console.enabled:true").applyTo(this.context);
131+
this.context.refresh();
132+
try (Connection connection = this.context.getBean(DataSource.class).getConnection()) {
133+
assertThat(capturedOutput).contains("Database available at '" + connection.getMetaData().getURL() + "'");
134+
}
135+
}
136+
116137
}

0 commit comments

Comments
 (0)