Results 1 to 8 of 8

Thread: Error on boolean constructor argument

  1. #1
    Join Date
    Aug 2004
    Location
    Amsterdam, The Netherlands
    Posts
    10

    Default 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?
    -paul.

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default IoC on CustomDateEditor/java.text.DateFormat

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

  3. #3
    Join Date
    Aug 2004
    Location
    Amsterdam, The Netherlands
    Posts
    10

    Default 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?
    -paul.

  4. #4
    Join Date
    Aug 2004
    Location
    Amsterdam, The Netherlands
    Posts
    10

    Default 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?
    -paul.

  5. #5
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    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

  6. #6
    Join Date
    Aug 2004
    Location
    Amsterdam, The Netherlands
    Posts
    10

    Default

    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!
    -paul.

  7. #7
    Join Date
    Aug 2004
    Location
    Amsterdam, The Netherlands
    Posts
    10

    Default

    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?
    -paul.

  8. #8
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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

Similar Threads

  1. Bind month and year to date
    By rcarver in forum Web
    Replies: 3
    Last Post: Jun 27th, 2006, 12:42 PM
  2. how to define Boolean in application context
    By commandos in forum Container
    Replies: 2
    Last Post: Jul 12th, 2005, 01:55 AM
  3. Replies: 4
    Last Post: May 13th, 2005, 03:33 AM
  4. Problems with boolean fields and Spring:bind
    By alfa_schumi in forum Web
    Replies: 4
    Last Post: Dec 9th, 2004, 05:04 AM
  5. Help: the boolean property exception!
    By linwei in forum Swing
    Replies: 2
    Last Post: Nov 17th, 2004, 07:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •