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) ...
CodePudding user response:
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.