1

Kofu Functional Bean DSL をいじっています。Spring-MVC で Spring-Data-JDBC を使用し、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.

助けてください、ありがとう

4

1 に答える 1