Results 1 to 7 of 7

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

  1. #1
    Join Date
    Jun 2007
    Posts
    8

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2007
    Posts
    8

    Lightbulb

    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

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    That is a correct assumption. Spring just follows the normal JavaBean specifications, nothing more nothing less.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote 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

  6. #6
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Quote 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

  7. #7
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote 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:

    Quote 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

Posting Permissions

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