【发布时间】:2021-02-05 19:37:56
【问题描述】:
我的应用程序是用spring-webmvc 和spring-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