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

BeanCreationException when using c-namespace, Kotlin class with default constructor and annotation configuration [SPR-16289] #20836

Closed
spring-issuemaster opened this issue Dec 11, 2017 · 1 comment

Comments

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

commented Dec 11, 2017

Alexander Chernikov opened SPR-16289 and commented

#20571 is fixed in Spring 5.0.2.
However, I've found minor sub-case when using c-namespace.

Dummy Kotlin:

package cr.spr16022

import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.ImportResource
import org.springframework.context.support.ClassPathXmlApplicationContext
import org.springframework.context.annotation.AnnotationConfigApplicationContext

class KotlinCtor(val intVal: Int) {
    constructor(p: String) : this(p.length)
}

class KotlinQtor() {
    constructor(p: String) : this()
}

@Configuration @ImportResource("cr.xml") class KotlinAnnotator

object Main {
    @JvmStatic fun main(args: Array<String>) {
        val contextXml = ClassPathXmlApplicationContext("cr.xml")
//        val contextAnn = AnnotationConfigApplicationContext(KotlinAnnotator::class.java)
    }
}

Beans definition:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="cr.spr16022.KotlinCtor" name="ctor0s" c:p="a"/>
    <bean class="cr.spr16022.KotlinQtor" name="qtor0s" c:p="a"/>
</beans>

Run this dummy main(): no problems.
Uncomment contextAnn declaration and initialization, re-run: fail:

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'qtor0s' defined in class path resource [cr.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'qtor0s' defined in class path resource [cr.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:243)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1270)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:758)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
	at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:88)
	at cr.spr16022.Main.main(KotlinCtor.kt:22)

Process finished with exit code 1

Affects: 5.0.2

Issue Links:

  • #20571 Avoid implicit autowiring with Kotlin secondary constructors

Referenced from: commits 19a1477

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Feb 1, 2018

Alexander Chernikov commented

Thank you, works for me in 5.0.3.

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.