16
16
17
17
package org .springframework .boot .autoconfigure .h2 ;
18
18
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 ;
19
26
import org .h2 .server .web .WebServlet ;
20
27
28
+ import org .springframework .beans .factory .ObjectProvider ;
29
+ import org .springframework .boot .autoconfigure .AutoConfigureAfter ;
21
30
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
22
31
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
23
32
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
24
33
import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication ;
25
34
import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication .Type ;
35
+ import org .springframework .boot .autoconfigure .jdbc .DataSourceAutoConfiguration ;
26
36
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
27
37
import org .springframework .boot .web .servlet .ServletRegistrationBean ;
28
38
import org .springframework .context .annotation .Bean ;
40
50
@ ConditionalOnWebApplication (type = Type .SERVLET )
41
51
@ ConditionalOnClass (WebServlet .class )
42
52
@ ConditionalOnProperty (prefix = "spring.h2.console" , name = "enabled" , havingValue = "true" , matchIfMissing = false )
53
+ @ AutoConfigureAfter (DataSourceAutoConfiguration .class )
43
54
@ EnableConfigurationProperties (H2ConsoleProperties .class )
44
55
public class H2ConsoleAutoConfiguration {
45
56
57
+ private static final Log logger = LogFactory .getLog (H2ConsoleAutoConfiguration .class );
58
+
46
59
@ Bean
47
- public ServletRegistrationBean <WebServlet > h2Console (H2ConsoleProperties properties ) {
60
+ public ServletRegistrationBean <WebServlet > h2Console (H2ConsoleProperties properties ,
61
+ ObjectProvider <DataSource > dataSource ) {
48
62
String path = properties .getPath ();
49
63
String urlMapping = path + (path .endsWith ("/" ) ? "*" : "/*" );
50
64
ServletRegistrationBean <WebServlet > registration = new ServletRegistrationBean <>(new WebServlet (), urlMapping );
@@ -55,6 +69,15 @@ public ServletRegistrationBean<WebServlet> h2Console(H2ConsoleProperties propert
55
69
if (settings .isWebAllowOthers ()) {
56
70
registration .addInitParameter ("webAllowOthers" , "" );
57
71
}
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
+ });
58
81
return registration ;
59
82
}
60
83
0 commit comments