0

Sorry for the copy of this article. I'd want to comment it, but without 50 scores of reputation I can`t comment, so...

I have

private boolean stopLoggingIntoDb;
....
  public void setStopLoggingIntoDb(String stopLoggingIntoDb) {  
    this.stopLoggingIntoDb = BooleanUtils.toBoolean(stopLoggingIntoDb.replaceAll("[^A-Za-z]", ""));
    logger.warn("Logging into SiebelMethodLogs is " + (!this.stopLoggingIntoDb ? "ON" : "OFF"));
}

and XML

<bean id="siebelMethodProcessor" class="com.entities.utils.Logger">
    <property name="logService" ref="logService"/>
    <property name="stopLoggingIntoDb" value="${monitor.siebel.stopLogging}"/>
</bean>

In that case, is everything Ok, but If I change the property in setter method from stopLoggingIntoDb to stopLog and change the property name in XML also to stopLog or not, Spring said me Invalid property 'stopLoggingIntoDb' or Bean property 'stopLog' is not writable.

Because of that, my question is What the Spring does with setter method? Which value is injected and which field/property is searching while gets the injection?

Dred
  • 1,076
  • 8
  • 24

1 Answers1

1

As can be seen in this example in the Spring Documentation, the name attribute of the <property> element must match a setter method. The name of the methods parameter and the name of the field doesn't matter.

Examples of dependency injection

The following example uses XML-based configuration metadata for setter-based DI. A small part of a Spring XML configuration file specifies some bean definitions:

<bean id="exampleBean" class="examples.ExampleBean">
    <!-- setter injection using the nested ref element -->
    <property name="beanOne">
        <ref bean="anotherExampleBean"/>
    </property>

    <!-- setter injection using the neater ref attribute -->
    <property name="beanTwo" ref="yetAnotherBean"/>
    <property name="integerProperty" value="1"/>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {

    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;

    public void setBeanOne(AnotherBean beanOne) {
        this.beanOne = beanOne;
    }

    public void setBeanTwo(YetAnotherBean beanTwo) {
        this.beanTwo = beanTwo;
    }

    public void setIntegerProperty(int i) {
        this.i = i;
    }

}

Notice how name="integerProperty" matches up to the setIntegerProperty() method, even though the parameter is named i and the field is named i.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • and how does Spring defines which setter should be? If I name property in Xml INTEGERproPErTY, so, which setter should be inside the Bean? It doesn`t matter, everytime it will be set(some name of property which start from upper case letter)? and, for my example it will be setINTEGERproPErTY or setIntegerProperty ? – Dred Mar 28 '19 at 07:03
  • @Dred It uses [**JavaBean naming convention**](https://stackoverflow.com/q/1991316/5221149), i.e. the name of a method starting with `set`, with exactly one parameter, the property name is the text after `set` with the first letter lowercased, and the property type is the type of the parameter. So if you name method `setINTEGERproPErTY`, the property name is `iNTEGERproPErTY` – Andreas Mar 28 '19 at 07:15