【发布时间】:2018-04-23 09:35:56
【问题描述】:
我遇到了 spring 和 kotlin 的问题:
这里是:
我有一个这样定义的 MyConfig 类
@Configuration
class MyConfig
@Bean
fun restTemplate(builder: RestTemplateBuilder): RestTemplate =
builder.build()
另一方面,我还定义了另一个 MyService 类
@Component
class MyService constructor(private val restTemplate: RestTemplate) {
fun test() {
// Use restTemplate
}
}
但我得到的只是以下消息:
Description:
Field restTemplate in my.package.MyService required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.
此问题仅发生在 @Configuration 类中定义的 bean(使用 @Bean 注释)而不是声明为 @Component 或 @Service 的 bean。
对于纯 Java 中的同类架构,我没有这样的问题。
- 我用的是spring boot 2.0.1
- kotlin 1.2.40
- 在 Java 8 上
那么,我错过了什么吗?
【问题讨论】: