Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
48adfe62
Commit
48adfe62
authored
Jun 14, 2019
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Log H2 console path and JDBC URL on startup
Closes gh-17063
parent
d1269845
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
1 deletion
+45
-1
H2ConsoleAutoConfiguration.java
...ork/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java
+24
-1
H2ConsoleAutoConfigurationTests.java
...oot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java
+21
-0
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java
View file @
48adfe62
...
...
@@ -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
;
}
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java
View file @
48adfe62
...
...
@@ -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
()
+
"'"
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment