如何在没有spring引导的情况下在spring-webmvc中启用h2控制台



我的应用程序是用spring-webmvc和不带spring-bootspring-jdbc构建的。在我的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包中添加了Serverbean,如下所示:

// 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,但我认为这不是一个解决方案,而是一个变通方法,因为我使用EmbeddedDatabaseBuilder添加了DataSourcebean,如下所示:

@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?或者这是启用它的正常方式?

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中使用H2控制台容器,如Tomcat或Jetty。

另请参阅:

  • org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration
  • org.h2.server.web.WebServlet

Spring Boot负责h2控制台servlet注册魔术,但使用香草弹簧(而不是弹簧引导(也很容易解决,使用任何"WebApplicationInitializer"的实现,如"AbstractSecurityWebApplicationInitialize"。例如,在servlet 3.0+环境(无web.xml(中添加调度器servlet和h2控制台:

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引导,您将需要为整个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/*");,请记住通过类似http://localhost:8080/<app-name>/console的url模式访问h2控制台

我的春季版本是5.3.16,h2是2.1.210

最新更新