Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 5.0.6
    • Fix Version/s: Waiting for Triage
    • Component/s: Core
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Hi,

      I am facing a strange behavior with dependency injection in my project, there is an unexpected initialization of one of our constructors.

      I am using Spring 5.0.6.RELEASE with XML configuration, we have the following structure:

      Entity P - this class depended on two entities and a String:

      public class P {public class P {
      
        public P(D1 d1, D2 d2, String input){ 
           System.out.println("P Constructor: D1: "+d1+", D2: "+d2+" , Input: "+input);
        }
      }
      

       Entity S - this class inject to the depended class, the class contains two constructors, we are                     initializing only the empty constructor

       

      public class S {public class S {
      
       public S() {
         System.out.println("S Empty Constructor!");
       }
      
       public S(String str) {
         System.out.println("S Argument Constructor!");
       }
      }
      

       

       Entity D1

      public class D1 {public class D1 {
      
       public D1(String input) {
         System.out.println("D1 Constructor with input: " + input);
       }
      
       public D1(S s, String input) {
         System.out.println("D1 Constructor with two inputs");
       }
      }
      

       

      Entity D2

       

      public class D2 {public class D2 {
      
        public D2(String input) {
           System.out.println("D2 Constructor with input: " + input);
       }
      
       public D2(S s, String input) {
          System.out.println("D2 Constructor with two inputs");
       }
      }
      

       

      XML configuration:

      <beans xmlns:context="http://www.springframework.org/schema/context"<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="        http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.0.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
      
       <bean id="p" class="com.zohar.P"> 
          <constructor-arg ref="d1" /> 
          <constructor-arg ref="d2" /> 
          <constructor-arg value="MS" /> 
      </bean>
      
       <bean id="d1" class="com.zohar.D1">
            <constructor-arg value="MS" />
       </bean> 
      
      <bean id="d2" class="com.zohar.D2">
        <constructor-arg  value="MS" /> 
      </bean>
       <bean id="s" class="com.zohar.S" />
      
      </beans>
      

       

       Based on the configuration file, the following activities should happen:

      1) S - calling to empty constructor.

      2) P - we are calling to the contractors with two depended and MS string.

      3) D2 - constructorr with one input 'MS'.

      4) D1 - constructore with input 'MS'

       

      The issue is that during the autowiring process Spring initializes the S constructor with the String argument which is not expected to be called.
      Program Output:
      S Argument Constructor!
      D1 Constructor with input: MS
      S Argument Constructor!
      D2 Constructor with input: MS
      P Constructor: D1: com.zohar.D1@4566e5bd, D2: com.zohar.D2@1ed4004b , Input: MS
      S Empty Constructor!
       
      This seems like a private case due to the second constructors that the depended class have (Seems like an issue with the reflection process).
        
      The solution to this issue is to add the wrapper class as a type to the 'constructor-arg' tag:
       
      <bean id="p" class="com.zohar.P">
      <constructor-arg ref="d1" />
      <constructor-arg ref="d2" />
      <constructor-arg  type="java.lang.String"  value="MS" />
      </bean>
       
      <bean id="d1" class="com.zohar.D1">
      <constructor-arg type="java.lang.String"  value="MS" />
      </bean>
      <bean id="d2" class="com.zohar.D2">
      <constructor-arg type="java.lang.String"  value="MS" />
      </bean>
       
      The question is: why we are having this issue in case the type is not set where it is not mandatory? 
       
      Attached is the zip with this example.
       
      Thank you very much in advance.
       

      Zohar

       

       

       

       

       

        Attachments

          Activity

            People

            • Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              zcohen Zohar Cohen
              Last updater:
              Spring Issuemaster
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since last comment:
                47 weeks, 1 day ago