My application is build with spring-webmvc
and spring-jdbc
without spring-boot
. In my application.properties
I have:
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
datasource.dbname=users
datasource.script=classpath:resources/users.sql
But it does not start h2-console
because I don't have spring-boot-devtools
, but do I need it? So I added Server bean from org.h2.tools
package like this:
// The web server is a simple standalone HTTP server that
// implements the H2 Console application. localhost:8082
@Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException {
return Server.createWebServer();
}
And now I can access web-console at localhost:8082
and connect to jdbc:h2:mem:users
, but I think this is not a solution, but a workaround, because I added the DataSource
bean using the EmbeddedDatabaseBuilder
like this:
@Bean
public DataSource dataSource(
@Value("${datasource.dbname}") String dbname,
@Value("${datasource.script}") String script) {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.setName(dbname)
.addScript(script)
.build();
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
Is there a spring way to enable h2-console
in spring-webmvc
without spring-boot
? Or is this the normal way to enable it?
pom.xml:
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- h2 database -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
<!-- servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>