gpt4 book ai didi

spring - Spring-fu-Kofu:无法连线 `NamedParameterJdbcTemplate`

转载 作者:行者123 更新时间:2023-12-02 13:32:42 27 4
gpt4 key购买 nike

我正在使用Kofu功能Bean DSL。我将Spring-Data-JDBC与Spring-MVC结合使用,并尝试自动连接NamedParameterJdbcTemplate。但是,我一直在收到这样的错误,即在运行测试时找不到适合它的bean。在基于注释的方法中,我们不必提供显式的NamedParameterJdbcTemplate。我的示例应用程序在这里:https://github.com/overfullstack/kofu-mvc-jdbc。 PFB从中得到一些代码片段:

val app = application(WebApplicationType.SERVLET) {
beans {
bean<SampleService>()
bean<UserHandler>()
}
enable(dataConfig)
enable(webConfig)
}
val dataConfig = configuration {
beans {
bean<UserRepository>()
}
listener<ApplicationReadyEvent> {
ref<UserRepository>().init()
}
}
val webConfig = configuration {
webMvc {
port = if (profiles.contains("test")) 8181 else 8080
router {
val handler = ref<UserHandler>()
GET("/", handler::hello)
GET("/api", handler::json)
}
converters {
string()
jackson()
}
}
}
class UserRepository(private val client: NamedParameterJdbcTemplate) {
fun count() =
client.queryForObject("SELECT COUNT(*) FROM users", emptyMap<String, String>(), Int::class.java)
}
open class UserRepositoryTests {
private val dataApp = application(WebApplicationType.NONE) {
enable(dataConfig)
}
private lateinit var context: ConfigurableApplicationContext
@BeforeAll
fun beforeAll() {
context = dataApp.run(profiles = "test")
}
@Test
fun count() {
val repository = context.getBean<UserRepository>()
assertEquals(3, repository.count())
}
@AfterAll
fun afterAll() {
context.close()
}
}

这是错误:
Parameter 0 of constructor in com.sample.UserRepository required a bean of type 'org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate' in your configuration.

请帮忙,谢谢

最佳答案

显然,Kofu不会从application.properties文件中选择数据源。一切都是声明性的,没有隐式派生。 (基本上没有Spring魔术🙂)。这为我工作:

val dataConfig = configuration {
beans {
bean {
val dataSourceBuilder = DataSourceBuilder.create()
dataSourceBuilder.driverClassName(“org.h2.Driver”)
dataSourceBuilder.url(“jdbc:h2:mem:test”)
dataSourceBuilder.username(“SA”)
dataSourceBuilder.password(“”)
dataSourceBuilder.build()
}
bean<NamedParameterJdbcTemplate>()
bean<UserRepository>()
}
listener<ApplicationReadyEvent> {
ref<UserRepository>().init()
}
}

关于spring - Spring-fu-Kofu:无法连线 `NamedParameterJdbcTemplate`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60360356/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com