Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Kotlin autowired constructor with optional parameters [SPR-15847] #20402

Closed
spring-issuemaster opened this issue Aug 4, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Aug 4, 2017

Abhijit Sarkar opened SPR-15847 and commented

@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:1242) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1141) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
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:84) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1234) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	... 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:79) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	... 17 common frames omitted

Affects: 5.0 RC3

Issue Links:

  • #20432 Revisit handling of missing fields (without default values) for immutable data classes
  • #20569 Streamline and reduce Kotlin delegates

Referenced from: commits ef68ccd

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Aug 22, 2017

Sébastien Deleuze commented

Abhijit Sarkar Could you please test latest snapshots to see if this commit provides the right behavior? (I did some integration tests but for such change an extra check on your side would be useful)

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Aug 24, 2017

Abhijit Sarkar commented

Verified that the application starts with Spring 5.0.0.BUILD-SNAPSHOT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.