只是把我的头撞到墙上,想分享结论:
正在使用的依赖项:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
</dependency>
<!-- and something to provide spring beans -->
</dependencies>
应用程序属性
spring.r2dbc.url=r2dbc:h2:tcp://localhost:9090/mem:mydb
# also works for file-based h2db
# spring.r2dbc.url=r2dbc:h2:tcp://localhost:9090/file./oreport
spring.r2dbc.username=sa
spring.r2dbc.password=
这个 bean 将创建您需要的 H2ConnectionFactory 实例。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.r2dbc.h2.H2ConnectionConfiguration;
import io.r2dbc.h2.H2ConnectionFactory;
@ConditionalOnProperty("spring.r2dbc.url")
@ConditionalOnExpression("#{'${spring.r2dbc.url}'.substring(0,12) matches 'r2dbc:h2:tcp'}")
@Configuration
public class H2Config {
/**
* As of 2021-08-06 the latest r2dbc-h2 version is 0.8.4.RELEASE and, even when
* tcp connections where enabled, there's still no auto-configuration. This Bean
* helps that.
*
* @see <a href="https://github.com/r2dbc/r2dbc-h2/issues/86">r2dbc-h2
* Issue#86</a>
*/
@Bean
public H2ConnectionFactory h2ConnectionFactory(@Value("${spring.r2dbc.url}") String url,
@Value("${spring.r2dbc.username}") String username, @Value("${spring.r2dbc.password}") String password) {
url = url.substring("r2dbc:h2:".length());
return new H2ConnectionFactory(
H2ConnectionConfiguration.builder().url(url).username(username).password(password).build());
}
}
此致
弗拉丹蒂姆