Ore*_*ron 0 spring inversion-of-control kotlin
对于使用 Spring/Kotlin 注入 bean,我知道两种方法:将其传递到构造函数中:
@Service
open class MyService @Autowired constructor(
@Autowired
val myRepository: MyRepository
)
Run Code Online (Sandbox Code Playgroud)
使用“lateinit”关键字:
@Service
open class MyService {
@Autowired
lateinit var myRepository: MyRepository
}
Run Code Online (Sandbox Code Playgroud)
我知道这两部作品,但我想知道哪一部最好?是否存在我可以使用一种解决方案而不是另一种解决方案遇到的问题?
谢谢 !
我更喜欢构造函数。如果只有一个构造函数,Spring 不再需要 @Autowired 注解。这样你就不必打开类(你可以打开一些Spring的东西,比如@Scheduled,但这是另一个问题)或使用var。它也很容易阅读。
这就是你所需要的
@Service
class MyService (private val myRepository: MyRepository)
| 归档时间: |
|
| 查看次数: |
4097 次 |
| 最近记录: |