user2681668
user2681668

Reputation: 191

How does Spring resolve case sensitive property names

package com.protect.shapes;

public class Triangle {
    String Type = null;
    String type = null;

    public String getType() {
        return Type;
    }

    public void setType(String Type) {
        this.Type = Type;
    }

    public String gettype() {
        return type;
    }

    public void settype(String type) {
        this.type = type;
    }

    public void draw() {
        System.out.println("Drawing " + type + " Triangle. " + Type);
    }
}

Configuration file.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Project beans go here -->
<bean id="triangle" class="com.protect.shapes.Triangle">
    <property name="Type" value="Equilateral">
    </property>
</bean>

I was trying to check how Spring handles case sensitive property names but could not figure it out: for both the type and Type fields I Set I get the same output

"Drawing Equilateral Triangle. null".

Can you please explain this why and how can I instantiate Type field.

Upvotes: 0

Views: 4091

Answers (1)

agnul
agnul

Reputation: 13048

Spring can't find a setter for your Type property:

  • setType won't do because according to the java beans specification that refers to the type property
  • settype is invalid because it's not recognized as a property setter according to the same specification.

If you want an upper case names property it should begin with (at least) two uppercase letters, e.g.

public void setPType(...) {
    ...
}

and

<property name="PType" value="..."/>

Check section 8.8 of the specification.

Upvotes: 1

Related Questions