https://kotlinlang.org logo
#spring
Title
m

minus

07/17/2017, 6:12 PM
Hello, I'm attempting to change Java 8 code to Kotlin, which includes changing field-based injection to constructor-based injection, but Spring is throwing me errors about missing default constructors. This isn't exactly a problem Kotlin specifically because it also fails when trying in Java, but maybe there's a trick to it that someone here knows. (Spring 4.2.6, Spring Boot 1.3.5)
n

nayanjyoti

07/17/2017, 6:17 PM
@minus : are the constructor based injections annotated with
@Autowired
m

minus

07/17/2017, 6:17 PM
in kotlin yes, in java it complained that @Autowired wasn't allowed with parameters
a

agomez

07/17/2017, 6:25 PM
did you added the
constructor
keyword as explained here https://kotlinlang.org/docs/reference/annotations.html#usage ?
m

minus

07/17/2017, 6:39 PM
ah yep, putting the annotation on the constructor instead of with the args (no compiler error on kotlin?) makes it work, thanks, @agomez @nayanjyoti
n

nfrankel

07/17/2017, 8:15 PM
@Autowired
on constructors is not required since latest versions of Spring try without 🙂
n

nerses

07/17/2017, 8:16 PM
@nfrankel it will require an upgrade to spring 4.3
@minus is using (Spring 4.2.6, Spring Boot 1.3.5)
n

nfrankel

07/17/2017, 8:49 PM
@nerses ok
3 Views