Autowired
alanında not eklenmiş Class'ta @Autowired alan oluşturmak Can not null
: Ben uygulamayı çalıştırdığınızdaKotlin @Configuration @EnableWebMvc
package com.lynas.config
import org.springframework.stereotype.Component
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
@Component
open class InterceptorConfig : HandlerInterceptorAdapter() {
override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any?): Boolean {
return true
}
}
package com.lynas.config
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.EnableWebMvc
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
@Configuration
@EnableWebMvc
@ComponentScan("com.lynas")
open class WebConfig() : WebMvcConfigurerAdapter() {
// this field show null
@Autowired
lateinit var interceptorConfig: InterceptorConfig
override fun addInterceptors(registry: InterceptorRegistry) {
registry.addInterceptor(interceptorConfig)
}
}
lateinit var interceptorConfig: InterceptorConfig
null
olduğunu. Bunu nasıl düzeltebilirim?
tam kod https://github.com/lynas/kotlinSpringBug
Spring Boot kullanıyor musunuz? Öyleyse, bootstrap sınıfınız nerede? Alt paketlerin içerisine kök paketinin ve diğer tüm bileşenlerin içine konulması önerilir. Belki de bileşen taramanız doğru çalışmıyordur. Ayrıca, '@ EnableWebMvc' ve' ComponentScan'ı tek bir '@ SpringBootApplication' ile değiştirerek de bırakabilirsiniz. Daha fazla bilgi için https://spring.io/blog/2016/02/15/developing-spring-boot-applications-with-kotlin adresine de bakınız. –
Yaylı olmayan önyükleme uygulaması – LynAs
'Autowired', Spring için uygun olan özel alanda olacak. Muhtemelen uygulamanızın geri kalanını kontrol etmelisiniz, sping config, sınıf yolu taraması, vs. Kotlin'e özgü bir sorun olan hiçbir şey görmüyorum. –