Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-16022

Avoid implicit autowiring with Kotlin secondary constructors

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 5.0 GA
    • Fix Version/s: 5.0.2
    • Component/s: None
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Looks similar to SPR-16012, which is marked fixed in 5.0, but I observe this with 5.0.0.RELEASE, so creating new issue for a case.

      Dummy Kotlin bean class:

      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      class KotlinCtor(val intVal: Int) {
          constructor(p: String) : this(p.length)
          constructor(p0: Int, p1: String) : this(p0 + p1.length)
      }
      

      Dummy beans in spring.xml:

      ...
          <bean class="temp.KotlinCtor" name="ctor0">
              <constructor-arg value="0"/>
          </bean>
          <bean class="temp.KotlinCtor" name="ctor1">
              <constructor-arg value="a"/>
          </bean>
          <bean class="temp.KotlinCtor" name="ctor2">
              <constructor-arg value="2"/>
              <constructor-arg value="b"/>
          </bean>
      ...
      

      Add configuration:

      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      @Configuration @ImportResource("spring.xml")
      open class KotlinAnnotator
      

      Dummy application, this time in Java:

      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class Main {
          public static void main(String[] args) {
              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
      //        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(KotlinAnnotator.class);
          }
      }
      

      Execute this main() method: no problem.
      Comment out XML context creation, uncomment annotation context creation, execute and fail:

      WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ctor1' defined in class path resource [spring.xml]: Unsatisfied dependency expressed through constructor parameter 0: Could not convert argument value of type [java.lang.String] to required type [int]: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "a"
      Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ctor1' defined in class path resource [spring.xml]: Unsatisfied dependency expressed through constructor parameter 0: Could not convert argument value of type [java.lang.String] to required type [int]: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "a"
      	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:691)
      	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1269)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1126)
      	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:756)
      	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 temp.Main.main(Main.java:9)
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              sdeleuze S├ębastien Deleuze
              Reporter:
              sashache Alexander Chernikov
              Last updater:
              Spring Issues Spring Issues
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since last comment:
                3 years, 19 weeks, 1 day ago