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

标签 spring spring-boot spring-mvc kotlin spring-data-jdbc

我正在使用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/

相关文章:

java - 'class "org.bouncycaSTLe.asn1.ASN1Primitive"'s signer information does not match signer information of other classes in the same package' 使用 Itext

java - `kotlinx.coroutines.withContext` 与 Spring WebFlux 一起使用是否安全?

java - 将 .war 文件部署到 tomcat 8 在 IDE 中工作正常但是当我部署到我的 VPS 时我丢失了所有的 JS 和 CSS

Spring 3 MVC : problem with form:errors and bindingresult

Spring MVC 3 : Is consumes supposed to disambiguate request mappings?

spring - 如何使用 Spring Cloud Netflix Feign 设置自定义 Jackson ObjectMapper

json - 使用 @ResponseBody 自定义 HttpMessageConverter 来做 Json 事情

spring-boot - Spring Boot 基本身份验证

java - 使用 Spring Boot 构建 Spring MVC 应用程序

java - Spring Security 中具有多个 http 部分的 NoUniqueBeanDefinitionException