Log H2 console path and JDBC URL on startup
Closes gh-17063
This commit is contained in:
@@ -16,13 +16,23 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.h2;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.h2.server.web.WebServlet;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -40,11 +50,15 @@ import org.springframework.context.annotation.Configuration;
|
||||
@ConditionalOnWebApplication(type = Type.SERVLET)
|
||||
@ConditionalOnClass(WebServlet.class)
|
||||
@ConditionalOnProperty(prefix = "spring.h2.console", name = "enabled", havingValue = "true", matchIfMissing = false)
|
||||
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
|
||||
@EnableConfigurationProperties(H2ConsoleProperties.class)
|
||||
public class H2ConsoleAutoConfiguration {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(H2ConsoleAutoConfiguration.class);
|
||||
|
||||
@Bean
|
||||
public ServletRegistrationBean<WebServlet> h2Console(H2ConsoleProperties properties) {
|
||||
public ServletRegistrationBean<WebServlet> h2Console(H2ConsoleProperties properties,
|
||||
ObjectProvider<DataSource> dataSource) {
|
||||
String path = properties.getPath();
|
||||
String urlMapping = path + (path.endsWith("/") ? "*" : "/*");
|
||||
ServletRegistrationBean<WebServlet> registration = new ServletRegistrationBean<>(new WebServlet(), urlMapping);
|
||||
@@ -55,6 +69,15 @@ public class H2ConsoleAutoConfiguration {
|
||||
if (settings.isWebAllowOthers()) {
|
||||
registration.addInitParameter("webAllowOthers", "");
|
||||
}
|
||||
dataSource.ifAvailable((available) -> {
|
||||
try (Connection connection = available.getConnection()) {
|
||||
logger.info("H2 console available at '" + path + "'. Database available at '"
|
||||
+ connection.getMetaData().getURL() + "'");
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
// Continue
|
||||
}
|
||||
});
|
||||
return registration;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,11 +16,21 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.h2;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.test.system.CapturedOutput;
|
||||
import org.springframework.boot.test.system.OutputCaptureExtension;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
|
||||
@@ -113,4 +123,15 @@ class H2ConsoleAutoConfigurationTests {
|
||||
assertThat(registrationBean.getInitParameters()).containsEntry("webAllowOthers", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExtendWith(OutputCaptureExtension.class)
|
||||
void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput capturedOutput) throws BeansException, SQLException {
|
||||
this.context.register(DataSourceAutoConfiguration.class, H2ConsoleAutoConfiguration.class);
|
||||
TestPropertyValues.of("spring.h2.console.enabled:true").applyTo(this.context);
|
||||
this.context.refresh();
|
||||
try (Connection connection = this.context.getBean(DataSource.class).getConnection()) {
|
||||
assertThat(capturedOutput).contains("Database available at '" + connection.getMetaData().getURL() + "'");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user