如何在没有spring-boot的情况下在spring-webmvc中启用h2-console? [英] How to enable h2-console in spring-webmvc without spring-boot?

查看:254
本文介绍了如何在没有spring-boot的情况下在spring-webmvc中启用h2-console?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是使用spring-webmvcspring-jdbc而不使用spring-boot构建的.在我的application.properties中,我有:

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

但是它没有启动h2-console,因为我没有spring-boot-devtools,但是我需要它吗?所以我从org.h2.tools包中添加了 Server bean :

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();
}

现在我可以在localhost:8082上访问Web控制台并连接到jdbc:h2:mem:users,但是我认为这不是解决方案,而是一种解决方法,因为我使用

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);
}

在没有spring-boot的情况下是否有春季方式spring-webmvc中启用h2-console?还是这是启用它的正常方法?

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>

推荐答案

似乎您需要在您的servlet映射中注册org.h2.server.web.WebServlet.

It appears you need to register a org.h2.server.web.WebServlet to your servlet mapping.

来自WebServlet的评论:

From the comments at WebServlet:

此Servlet允许在标准Servlet中使用H2控制台 容器,例如Tomcat或Jetty.

This servlet lets the H2 Console be used in a standard servlet container such as Tomcat or Jetty.

另请参阅:

这篇关于如何在没有spring-boot的情况下在spring-webmvc中启用h2-console?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆