@Component
class A(val str: String = "whatever", val b: B) {
// class A constructor(val str: String = "whatever", @Autowired val b: B) doesn't work either
}
@Component
class B {
}
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
SpringApplication.run(DemoApplication::class.java, *args)
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'a' defined in file [<elided>/A.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.demo.A]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.demo.A.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at ...<elided>
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.demo.A]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.demo.A.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
... 16 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.example.demo.A.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_131]
at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_131]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
... 17 common frames omitted
Comment From: wilkinsona
I can see from the stack trace that you are using Spring Framework 4.3. I suspect you'll need to use Spring Framework 5 (and, therefore Spring Boot 2.0) for this to work. Either way this is a Spring Framework issue and, if necessary, it should be reported using their JIRA.
Comment From: asarkar
Same thing with 5.0.0.RC3. Filed SPR-15847.
Comment From: jillesvangurp
Late to the party but ran into the same and figured out that the default value for the first param is the problem. If you remove this, it should work. One workaround is simply annotating using @Value("\${mystr:whatever}")
(escape the $
)
I have a Spring 4.x project with kotlin and some old version of jersey that I can't migrate to 5.x without a lot of work. With spring 5.x you can use a gradle plugin to make kotlin do the right thing with open classes and default constructors at compile time.
Comment From: kvmw
Late to the party but ran into the same and figured out that the default value for the first param is the problem. If you remove this, it should work. One workaround is simply annotating using
@Value("\${mystr:whatever}")
(escape the$
)I have a Spring 4.x project with kotlin and some old version of jersey that I can't migrate to 5.x without a lot of work. With spring 5.x you can use a gradle plugin to make kotlin do the right thing with open classes and default constructors at compile time.
That was exactly my issue too. having default value (kotlin default value) in constructor was causing this issue.