[Spring][Kotlin] 필드에 @Autowired 어떻게 사용하나?

Kotlin|2019. 9. 6. 19:02

아직 익숙하지 않지만, 코틀린을 조금씩 학습중에 @Autowired를 어떻게 써야하는지 난감한 상황이 생겼습니다.

 

자바에서는 일반적으로 

 

@Autowired
private EmpDao empDao;

형태로 간단히 사용할수 있지만

 

코틀린에서

 

@Autowired
var empDao: EmpDao;

'Property must be initialized or be abstract' 에러와 함께 동작하지 않았습니다.

 

그럴때는'lateinit' 을 사용하면 에러 없이 사용가능합니다.

@Autowire
lateinit var empDao : EmpDao;

 

참고 : https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties

'Kotlin' 카테고리의 다른 글

split과 동시에 assign  (0) 2021.11.29
코틀린 upgrade 1.3.7 to 1.5.0  (0) 2021.05.28
[Kotlin] default argument, named argument  (0) 2019.09.18
[Kotlin] 코틀린에 primitive가 없다고? 느리지 않을까?  (1) 2019.09.10
[Kotlin] data class  (0) 2019.09.09

댓글()