Announcement Announcement Module
Collapse
No announcement yet.
Error on boolean constructor argument Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error on boolean constructor argument

    Here's a strange error I got during context initialization on a CustomDateEditor bean.

    Code:
    <bean id="customDateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor">
       <constructor-arg><ref local="dateFormatSql"/></constructor-arg>
       <constructor-arg><value>true</value></constructor-arg>
    </bean>
    The error I got is with the second contructor argument:

    Code:
    org.springframework.beans.factory.UnsatisfiedDependencyException&#58; Error creating bean with name 'customDateEditor' defined in resource &#91;/WEB-INF/applicationContext.xml&#93; of ServletContext&#58; Unsatisfied dependency expressed through constructor argument with index 1 of type &#91;boolean&#93;&#58; Did you specify the correct bean references as generic constructor arguments?
    Well I did specify the correct constructor arguments right?

  • #2
    IoC on CustomDateEditor/java.text.DateFormat

    Can you post the code for the referenced bean, id="dateFormatSql"?

    Comment


    • #3
      Definition of referenced bean

      Here the definition of the referenced bean.
      Code:
      <bean id="dateFormatSql" class="java.text.SimpleDateFormat">
         <constructor-arg><value>yyyy-MM-dd</value></constructor-arg>
      </bean>
      This should be ok right? Note that the error reports the constructor arg with index 1 (the second) to be the problem.

      What am I missing here?

      Comment


      • #4
        Primitive boolean argument on TestClass

        It's getting really confusing; I've created a test class with a constructor with a single primitive boolean argument, defined in the bean container as this:
        Code:
        <bean id="testBean" class="nl.ctb.ewub.business.TestClass">
           <constructor-arg><value>true</value></constructor-arg>
        </bean>
        and it doesn't work as well! Did I misunderstand Spring being able to convert all primitives? How should this be done then? Well, I'm confused. Guys?

        Comment


        • #5
          I suggest you start using indexed constructor arguments. The support for detecting the argument types has it quirks sometimes.

          Code:
          <constructor-arg index="1"><ref bean="dateFormat"/></constructor-arg>
          <constructor-arg index="2"><value>true</value></constructor-arg>
          I think the index is one-based, but just try it out to be sure.

          Hope this helps.

          Alef

          Comment


          • #6
            Thank you Alef!

            That appears to solve the issue. The bean container initializes without errors when specifying a 0-based index on the <constructor-arg>. Here's the whole solution (for completeness):

            Code:
            <bean id="customDateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor">
                <constructor-arg index="0"><ref local="dateFormatSql"/></constructor-arg>
                <constructor-arg index="1"><value>true</value></constructor-arg>
            </bean>
            Guess this is a pitfall for others too, since the error message is a little bit misleading. The error states
            Unsatisfied dependency expressed through constructor argument with index 1 of type boolean
            . Thus the container correctly recognizes the second boolean argument with index 1. This is a clue that the correspondence between constructor arguments and <constructor-args> element is not ambigious for the container. Nonetheless providing explicit index info solves the issue!

            Alef -- thanks again!

            Comment


            • #7
              As a post scriptum: the index attributes on a <constructor-arg> seem to be an undocumented container feature. It appears only in the bean DTD. Shouldn't this be in the section (3.3.1) about type 3 IoC?

              Comment


              • #8
                the index attributes on a <constructor-arg> seem to be an undocumented
                Yes, it probably should be better documented - I'll post a bug (hopefully with doco patch - ). In addition the above values can be resolved by specifying a type. E.g.
                Code:
                <constructor-arg><ref bean="dateFormat"/></constructor-arg>
                <constructor-arg type="boolean"><value>true</value></constructor-arg>
                I think the index is one-based
                It's actually 0 based

                Comment

                Working...
                X