kotlin+Autowired报错lateinit property has not been initialized?

初学springboot,被mybatis-plus难住了。

我用Kotlin写的springboot项目,用mybatis-plus操作数据库,用@Autowired注入BaseMapper,但是在运行的时候却提示lateinit property registerUserDao has not been initialized,注入的对象没有被初始化。请问问题可能会出现在哪里呢?

项目结构

启动类:

@SpringBootApplication

@ComponentScan(value = ["com.jingyin.imusic_server_dao", "com.jingyin.imusic_server_api"]) // 扫描子模块中的RestController

@MapperScan(basePackages = ["com.jingyin.imusic_server_dao"])

class ImusicServerApplication

fun main(args: Array<String>) {

runApplication<ImusicServerApplication>(*args)

println("启动成功!")

}

数据库entity:

@TableName(value = "user_auth")

class DbUserAuth {

@TableId(type = IdType.AUTO)

var id: Long? = null

var phone: String? = null

var password: String? = null

var token: String? = null

@TableLogic

var deleted: Int? = null

companion object {

fun new(

id: Long?,

phone: String?,

password: String?,

token: String?,

): DbUserAuth = DbUserAuth().apply {

this.id = id

this.phone = phone

this.password = password

this.token = token

this.deleted = 0

}

}

}

dao调用数据库:

@Repository

public open interface RegisterUserMapper: BaseMapper<DbUserAuth> {

fun queryByPhone(@Param("phone") phone: String): DbUserAuth? =

selectOne(QueryWrapper<DbUserAuth>().eq("phone", phone))

}

service调用dao实现业务:

@Component

class RegisterUserService {

这里报错:未被初始化

这里报错:未被初始化

这里报错:未被初始化

@Autowired

private lateinit var registerUserDao: RegisterUserMapper

/**

* @description: 查看是[phone]否有对应的用户

* @author: lizhenxin

* @date: 2022/10/13 下午5:04

* @param phone 手机号码

* @return 如果有注册用户,则返回该用户;否则返回空

**/

open fun checkLogin(phone: String): DbUserAuth? =

registerUserDao.queryByPhone(phone)

open fun insertRegisterUser(user: DbUserAuth) {

registerUserDao.let {

it.insert(user).run { println("insertRegisterUser result: $this") }

}

}

}

调用service的地方:

@RestController

@RequestMapping(value = ["/auth"])

class AuthApi {

private val userService: RegisterUserService by lazy { RegisterUserService() }

/**

* @description: 用户登录

* @author: lizhenxin

* @date: 2022/10/13 下午5:57

* @param

* @return

**/

@PostMapping(value = ["/login"])

fun login(@RequestBody request: String): String {

val user = request.asJson<LoginInfo>()

println("request: ${request}")

println("login info: ${user}")

return userService.checkLogin(user.phone)

?.let { "登录成功" }

?: let {

userService.insertRegisterUser(createNewUser(user))

"注册为新用户"

}

}

private fun createNewUser(loginInfo: LoginInfo): DbUserAuth = DbUserAuth.new(

id = 0,

phone = loginInfo.phone,

password = loginInfo.password,

token = "sfdsdfsdf",

)

}

报错:

kotlin.UninitializedPropertyAccessException: lateinit property registerUserDao has not been initialized

at com.jingyin.imusic_server_service.authentication.RegisterUserService.checkLogin(RegisterUserService.kt:36) ~[main/:na]

at com.jingyin.imusic_server_api.authentication.AuthApi.login(AuthApi.kt:39) ~[main/:na]

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]

at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]

at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.23.jar:5.3.23]

at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.23.jar:5.3.23]

at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.23.jar:5.3.23]

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.23.jar:5.3.23]

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.23.jar:5.3.23]

at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.23.jar:5.3.23]

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071) ~[spring-webmvc-5.3.23.jar:5.3.23]

at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964) ~[spring-webmvc-5.3.23.jar:5.3.23]

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.23.jar:5.3.23]

at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.23.jar:5.3.23]

at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) ~[tomcat-embed-core-9.0.65.jar:4.0.FR]

at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.23.jar:5.3.23]

at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.65.jar:4.0.FR]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.65.jar:9.0.65]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.23.jar:5.3.23]

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.23.jar:5.3.23]

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.23.jar:5.3.23]

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.65.jar:9.0.65]

at java.base/java.lang.Thread.run(Thread.java:839) ~[na:na]

求大神指点我一下应该怎么排查这个错误,刚学springboot实在是无力啊


回答:

API里的service不是bean,不受spring管理,里面的dao也就没有注入
spring是可以注入kotlin的构造方法的,于是 RegisterUserService(val registerUserDao: RegisterUserMapper) 就可以了


建议要么学kotlin要么学spring,这俩一起来学不明白太正常了
另外kotlin长期看服务端应用有限,学了也用不上……除非做安卓

以上是 kotlin+Autowired报错lateinit property has not been initialized? 的全部内容, 来源链接: utcz.com/p/944767.html

回到顶部