Given the following extract from a ejb singleton bean:
import javax.annotation.PostConstruct as postConstruct
import javax.inject.Inject as inject
import javax.ejb.Singleton as singleton
import javax.ejb.Startup as startup
singleton
startup
class ExampleBean {
inject var log: Logger? = null
postConstruct
public fun doIt() {
log!!.info(“a log string”)
}
//...
}
Although I know that the injected log will practically never be null, I still have to declare it as a var and as nullable. As a result I have to safeguard every single access. Is there a pattern to make this a bit more elegant? I was trying to use Constructor injection instead but no luck:
WELD-000816 Cannot determine constructor to use for public final@Singleton @JetClass@Startup class …ExampleBean
The part about constructor injection with EJB session beans is now clearer: The EJB 3.1 spec, section 4.9.2 states
The class must have a public constructor that takes no parameters.
Using Kotlin, this means having either a constructor taking no parameters or having a constructor with all parameters defaulted.
Either way, that does not free us from the unnecessary !! checks.
Next I was trying to get rid of the null checks using a CDI Bean. Goal: have a constructor which frees us from null checks like
inject class ExampleBean(val log: Logger) {
I could not get this to work. When using constructor injection, I had to default all parameters to make it pass the cdi checks. Finally, cdi chose the default constructor and nothing was injected.
If you are starting a new project in Kotlin and not carrying over existing use of Dagger, I would recommend looking at Kodein which is DI written specifically for Kotlin: GitHub - SalomonBrys/Kodein