Announcement Announcement Module
Collapse
No announcement yet.
Can't inject property values - but refs and constructor args ok Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't inject property values - but refs and constructor args ok

    Hi,

    I am trying to inject text values into fields in a class using property/value but I get InvalidPropertyException. I'm guessing I should be using something else (have tried 'field' but this isn't recognised) or my syntax is wrong.

    Note if I inject as a constructor argument it works fine, and injecting references to other beans works ok too. The error is specific to <property name="xxx" value="yyy" />.


    Can someone advise what I am doing wrong here?

    Thanks




    My XML looks something like:

    <bean id="injected_class"
    class="com.my.class.to.inject.into">
    <constructor-arg ref="db_conn" /> <!-- works ok -->
    <property name="some_class" ref="sc" /> <!-- works ok -->
    <property name="logger" ref="logger" /> <!-- works ok -->
    <property name="applicationCode" value="abc123" />
    <!-- FAILS, but there is definitely an 'applicationCode' (String) private instance variable -->
    </bean>

    ------------------------------------------------------------

    Class:

    ...

    private String applicationCode = null;

    ...

    public void setWorkQApplicationCode(String app_code)
    {
    applicationCode = app_code;
    }

    ...

  • #2
    Spring locates the properties by there setters. So when you are injecting something into a property applicationCode spring (as per JavaBean spec) tries to set it and looks for a setApplicationCode method. Your method is called setWorkQApplicationCode which isn't going to match.

    2 solutions
    1) Change your setWorkQApplicationCode to setApplicationCode
    2) rename your property to workQApplicationCode

    Comment


    • #3
      I thought the field names were determined by reflection and didn't think the name of the setter was relevant but now see what I misunderstood.

      If the setter naming has to be consistent with the property name am I right in assuming auto-wiring uses the same convention?

      Many thanks

      Comment


      • #4
        That is a correct assumption. Spring just follows the normal JavaBean specifications, nothing more nothing less.

        Comment


        • #5
          Originally posted by dave_gr View Post
          If the setter naming has to be consistent with the property name
          Actually the setter does not need to consistent with the property name. Spring uses only the setter/getter, not the property at all. So you can name the latter as you want. And you don't need to change your Java code, only the name of the property in the XML config.

          Jörg

          Comment


          • #6
            Originally posted by Jörg Heinicke View Post
            Actually the setter does not need to consistent with the property name. Spring uses only the setter/getter, not the property at all. So you can name the latter as you want. And you don't need to change your Java code, only the name of the property in the XML config.

            Jörg
            Sorry, but your explanation is slightly bizarre. You have mixed-up property and field. Property is defined by getter/setter pair (in which one of the r members may absent). And it may be and may be not backed-uip by field (of the same or different name). Just look chapter 7.1 of JavaBeans specification.

            Theoretically, it is possible to have arbitrary getter/setter names for a property of a given name (by providing this information in the BeanInfo class related to your class, for details see above mentioned specification). but practically always property name imatches from getter/setter name,
            getPropertyName(), setPropertyName(). See chapter 8.3 of specification.

            BTW, I'm not completely sure if Spring supports properties for which getter/setter names deviate from standard convention.

            Regards,
            Oleksandr

            Comment


            • #7
              Originally posted by al0 View Post
              You have mixed-up property and field.
              You are right, sorry for that. Knowing what he actually meant I followed to closely Dave's statement:

              Originally posted by dave_gr View Post
              If the setter naming has to be consistent with the property name
              Pointing out the difference between field and property would have been the way to go. Actually with clearly separating field from property the above statements makes not much sense since a property is defined by its getter/setter.

              Jörg

              Comment

              Working...
              X