我如何绑定两个kotlin文件
我正在与Java和Kotlin的一个项目合作。 该项目最初仅在Java。我正在将某些课程迁移到Kotlin。但是我在这个课程中遇到了我迁移到科特林的问题。此问题在配置文件中。
配置文件。
package com.fourtwenty.core
import ...
class CoreModule : AbstractModule() {
override fun configure() {
// Repositories
bind(AppRepository::class.java).to(AppRepositoryImpl::class.java)
...
// Services
bind(AuthenticationService::class.java).to(AuthenticationServiceImpl::class.java)
bindingClassimpl
package com.fourtwenty.core.services.mgmt.impl
import ...
open class AuthenticationServiceImpl @Inject constructor(token: Provider<ConnectAuthToken>) : AbstractAuthServiceImpl(token), AuthenticationService {
@Inject
private val metrcAccountRepository: MetrcAccountRepository? = null
@Inject
private val stripeService: StripeService? = null
@Inject
var integrationSettingRepository: IntegrationSettingRepository? = null
@Inject
private val shopPaymentOptionRepository: ShopPaymentOptionRepository? = null
@Inject
private val amazonServiceManager: AmazonServiceManager? = null
...
bindingClass
package com.fourtwenty.core.services.mgmt
import ...
interface AuthenticationService {
fun getCurrentActiveEmployee(): InitialLoginResult?
fun adminLogin(request: EmailLoginRequest?): InitialLoginResult?
...
,我在Intellij中遇到了此错误
1) Injected field com.fourtwenty.core.services.mgmt.impl.AuthenticationServiceImpl.metrcAccountRepository cannot be final.
at com.fourtwenty.core.CoreModule.configure(CoreModule.kt:1266)
2) Injected field com.fourtwenty.core.services.mgmt.impl.AuthenticationServiceImpl.stripeService cannot be final.
at com.fourtwenty.core.CoreModule.configure(CoreModule.kt:1266)
3) Injected field com.fourtwenty.core.services.mgmt.impl.AuthenticationServiceImpl.shopPaymentOptionRepository cannot be final.
at com.fourtwenty.core.CoreModule.configure(CoreModule.kt:1266)
4) Injected field com.fourtwenty.core.services.mgmt.impl.AuthenticationServiceImpl.amazonServiceManager cannot be final.
at com.fourtwenty.core.CoreModule.configure(CoreModule.kt:1266)
5) ...
I'm working with a project in Java and Kotlin.
This project was originally in java only. And I'm doing a migration of some classes to kotlin. But I have this problem in this class that I migrated to kotlin. This problem is in a configure file.
Configure file.
package com.fourtwenty.core
import ...
class CoreModule : AbstractModule() {
override fun configure() {
// Repositories
bind(AppRepository::class.java).to(AppRepositoryImpl::class.java)
...
// Services
bind(AuthenticationService::class.java).to(AuthenticationServiceImpl::class.java)
BindingClassImpl
package com.fourtwenty.core.services.mgmt.impl
import ...
open class AuthenticationServiceImpl @Inject constructor(token: Provider<ConnectAuthToken>) : AbstractAuthServiceImpl(token), AuthenticationService {
@Inject
private val metrcAccountRepository: MetrcAccountRepository? = null
@Inject
private val stripeService: StripeService? = null
@Inject
var integrationSettingRepository: IntegrationSettingRepository? = null
@Inject
private val shopPaymentOptionRepository: ShopPaymentOptionRepository? = null
@Inject
private val amazonServiceManager: AmazonServiceManager? = null
...
BindingClass
package com.fourtwenty.core.services.mgmt
import ...
interface AuthenticationService {
fun getCurrentActiveEmployee(): InitialLoginResult?
fun adminLogin(request: EmailLoginRequest?): InitialLoginResult?
...
And I got this error in IntellIJ
1) Injected field com.fourtwenty.core.services.mgmt.impl.AuthenticationServiceImpl.metrcAccountRepository cannot be final.
at com.fourtwenty.core.CoreModule.configure(CoreModule.kt:1266)
2) Injected field com.fourtwenty.core.services.mgmt.impl.AuthenticationServiceImpl.stripeService cannot be final.
at com.fourtwenty.core.CoreModule.configure(CoreModule.kt:1266)
3) Injected field com.fourtwenty.core.services.mgmt.impl.AuthenticationServiceImpl.shopPaymentOptionRepository cannot be final.
at com.fourtwenty.core.CoreModule.configure(CoreModule.kt:1266)
4) Injected field com.fourtwenty.core.services.mgmt.impl.AuthenticationServiceImpl.amazonServiceManager cannot be final.
at com.fourtwenty.core.CoreModule.configure(CoreModule.kt:1266)
5) ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从来没有使用过Guice(我认为这是Guice,因为它没有标记),但是出现与Spring相同的问题,因此应适用相同的解决方案。
这些字段为
val
,因此初始化为null后,DI框架无法修改它们。他们将需要成为
var
,以便以后可以更新。最重要的是,您可能确定大多数或所有这些字段都不会无效,因此可以使用LateInit关键字来规避详细的无效检查。实际上,DI是Lateinit关键字存在的主要用例之一: https://kotlinlang.org/docs/properties.html#late-initialized-properties-properties-and-variobles
,它会变成:
请注意 : /code>)是在您的情况下进行构造函数注入。
I've never used Guice (I'm assuming this is guice, since it isn't tagged), but the same issue arises as with spring so the same solution should apply.
The fields are
val
so they can't be modified by the DI framework after being initialized to null.They will need to become
var
so they can be updated afterwards.On top of that you are probably certain that most or all of those fields won't be null, so the verbose nullability checks could be circumvented using the lateinit keyword. In fact DI is one of the main use cases mentioned for the existence of the lateinit keyword: https://kotlinlang.org/docs/properties.html#late-initialized-properties-and-variables
So it would become:
Do note that a cleaner alternative (with
val
) is to do constructor injection, if possible in your situation.