Jun 28th, 2007, 09:44 AM
Can't inject property values - but refs and constructor args ok
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?
My XML looks something like:
<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 -->
private String applicationCode = null;
public void setWorkQApplicationCode(String app_code)
applicationCode = app_code;
Jun 28th, 2007, 10:01 AM
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.
1) Change your setWorkQApplicationCode to setApplicationCode
2) rename your property to workQApplicationCode
Jun 29th, 2007, 03:04 AM
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?
Jun 29th, 2007, 03:35 AM
That is a correct assumption. Spring just follows the normal JavaBean specifications, nothing more nothing less.
Jun 29th, 2007, 08:09 AM
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.
Originally Posted by dave_gr
Jun 29th, 2007, 09:40 AM
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.
Originally Posted by Jörg Heinicke
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.
Jun 29th, 2007, 12:06 PM
You are right, sorry for that. Knowing what he actually meant I followed to closely Dave's statement:
Originally Posted by al0
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.
Originally Posted by dave_gr