我如何绑定两个kotlin文件

发布于 2025-01-21 08:09:48 字数 2228 浏览 0 评论 0原文

我正在与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

禾厶谷欠 2025-01-28 08:09:48

我从来没有使用过Guice(我认为这是Guice,因为它没有标记),但是出现与Spring相同的问题,因此应适用相同的解决方案。

这些字段为val,因此初始化为null后,DI框架无法修改它们。

他们将需要成为var,以便以后可以更新。

最重要的是,您可能确定大多数或所有这些字段都不会无效,因此可以使用LateInit关键字来规避详细的无效检查。实际上,DI是Lateinit关键字存在的主要用例之一: https://kotlinlang.org/docs/properties.html#late-initialized-properties-properties-and-variobles

,它会变成:

private lateinit var metrcAccountRepository: MetrcAccountRepository

请注意 : /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:

private lateinit var metrcAccountRepository: MetrcAccountRepository

Do note that a cleaner alternative (with val) is to do constructor injection, if possible in your situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文