【发布时间】:2020-10-14 08:06:23
【问题描述】:
我有一个 Springboot 项目,作为测试的一部分,我使用嵌入式 Redis 服务器。当我使用 Intellij 运行特定的测试类时,它工作正常并且测试通过。但是当我使用mvn clean test 命令运行测试时,我收到以下错误:
BeanCreationException:创建带有名称的 bean 时出错 'core.config.TestRedisConfiguration':调用 init 方法 失败的;嵌套异常是 java.lang.RuntimeException: Can't start redis 服务器。查看日志了解详情。
这是我的 pom.xml 文件中的依赖项:
<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.6</version>
<scope>test</scope>
</dependency>
这是我的 TestRedisConfiguration 类:
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.TestConfiguration
import redis.embedded.RedisServer
import redis.embedded.RedisServerBuilder
import javax.annotation.PostConstruct
import javax.annotation.PreDestroy
@TestConfiguration
class TestRedisConfiguration(@Value("\${redis.port:63799}") private val port: Int) {
private val redisServer: RedisServer
init {
redisServer = RedisServerBuilder().port(port).build()
}
@PostConstruct
fun postConstruct() {
redisServer.start()
}
@PreDestroy
fun preDestroy() {
redisServer.stop()
}
}
【问题讨论】:
-
@Value("\${redis.port:63799}"中的 \ 是错字吗? -
@Smile 你指的是哪一部分?我故意使用 63799 端口而不是默认的 6379
-
使用 kotlin 时,\ 是必要的转义
-
啊,好吧,我的错!
-
\ 用于转义那里的
$,默认情况下,$ 符号计算{}内的表达式并将结果放入字符串中。
标签: java spring spring-boot kotlin redis