【问题标题】:How to enable h2-console in spring-webmvc without spring-boot?如何在没有 spring-boot 的情况下在 spring-webmvc 中启用 h2-console?
【发布时间】:2021-02-05 19:37:56
【问题描述】:

我的应用程序是用spring-webmvcspring-jdbc 构建的,没有spring-boot。在我的application.properties 我有:

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,如下所示:

// 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 访问网络控制台并连接到jdbc:h2:mem:users,但我认为这不是解决方案,而是一种解决方法,因为我使用EmbeddedDatabaseBuilder 添加了DataSource bean,就像这样:

@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 方式在没有spring-boot 的情况下在spring-webmvc 中启用h2-console?还是这是启用它的正常方式?

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>

【问题讨论】:

    标签: java spring spring-mvc h2 spring-jdbc


    【解决方案1】:

    Spring Boot 负责 h2-console servlet 注册魔法,但使用 vanilla spring(不是 spring-boot)也很容易解决,使用“WebApplicationInitializer”的任何实现,例如“AbstractSecurityWebApplicationInitializer”。例如,在 Servlet 3.0+ 环境(无 web.xml)中添加调度程序 servlet 和 h2-console:

    public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
    
        @Override
        protected void beforeSpringSecurityFilterChain(ServletContext container) {
            AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
            context.register(AppConfig.class, SecurityConfig.class, WebConfig.class);
            context.setServletContext(container);
            ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(context));
            servlet.setInitParameter("throwExceptionIfNoHandlerFound", "true");
            servlet.setLoadOnStartup(1);
            servlet.addMapping("/");
    
            ServletRegistration.Dynamic h2Console = container.addServlet("h2-console", new WebServlet());
            h2Console.setInitParameter("-webAllowOthers", "");
            h2Console.setLoadOnStartup(1);
            h2Console.addMapping("/h2/*", "/h2-console/*");
        }
    }
    

    如果没有 spring-boot,您将需要为整个 Spring web 层手动配置 maven(或 gradle)依赖项,包括 Tomcat 所需的那些库(如果未嵌入),当然还有 h2 依赖项:

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>${h2.version}</version>
    </dependency>
    

    【讨论】:

      【解决方案2】:

      看来您需要将 org.h2.server.web.WebServlet 注册到您的 servlet 映射。

      来自 WebServlet 的 cmets:

      此 servlet 允许在标准 servlet 中使用 H2 控制台 Tomcat 或 Jetty 等容器。

      另见:

      【讨论】:

        猜你喜欢
        • 2020-12-23
        • 2018-07-11
        • 1970-01-01
        • 2017-01-16
        • 2018-11-18
        • 2016-10-26
        • 2020-04-24
        • 2018-03-31
        • 2016-09-20
        相关资源
        最近更新 更多