簡體   English   中英

如何在沒有 spring-boot 的情況下在 spring-webmvc 中啟用 h2-console?

[英]How to enable h2-console in spring-webmvc without spring-boot?

我的應用程序是使用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 package 添加了服務器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>

看來您需要將org.h2.server.web.WebServlet注冊到您的 servlet 映射。

來自 WebServlet 的評論:

此 servlet 允許在標准 servlet 容器(例如 Tomcat 或 Jetty)中使用 H2 控制台。

也可以看看:

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>

首先,你需要定義dataSource

    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
            .generateUniqueName(false)
            .setName("testdb")
            .setType(EmbeddedDatabaseType.H2)
            .setScriptEncoding("UTF-8")
            .ignoreFailedDrops(true)
            .addScripts("sqlscripts/schema.sql", "sqlscripts/data.sql")
            .build();
    }

然后你可以實現WebApplicationInitializer並覆蓋onStartup

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.h2.server.web.WebServlet;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

public class ApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //bootstrap dispatcher servlet
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(ApplicationConfig.class); // whatever config that you want to register

        ServletRegistration.Dynamic h2ServletRegistration = servletContext.addServlet(
            "h2-console",
            new WebServlet()
        );
        h2ServletRegistration.setLoadOnStartup(1);
        h2ServletRegistration.addMapping("/console/*");
    }
}

並且由於您使用h2ServletRegistration.addMapping("/console/*"); ,記得通過 url 模式訪問 h2-console,例如http://localhost:8080/<app-name>/console

我的spring版本是5.3.16 ,h2是2.1.210

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM