Announcement Announcement Module
Collapse
No announcement yet.
Injecting more than single value in a Constructor Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injecting more than single value in a Constructor

    Hi,
    Is there a way of setting more than one property in C'tor without using setter methods?
    Eg:
    public class Employee{
    String firstName;
    String lastName;
    public Employee(String firstName,String lastName){
    this.firstName= firstName;
    this.lastName = lastName;
    }
    If yes then how would we differentiate in the below xml ?
    <beans>
    <bean id="blah" class="x.x.Employee">
    <constructor-arg value="Senthil Kumar" />
    <constructor-arg value="Sekar" />
    </beans>

    Thanks.

    Regards
    Senthil Kumar Sekar

  • #2
    Look in the Spring doc for the "Constructor argument resolution" section. This describes the various ways to remove the ambiguity. You'll probably use the "index" feature.

    Comment


    • #3
      <bean id="blah" class="x.x.Employee">
      <constructor-arg index="0" value="Senthil Kumar"/>
      <constructor-arg index="1" value="Sekar"/>
      </bean>

      Comment


      • #4
        It's worth noting that if you don't use the index attribute, it's purely based on the order of the constructor-arg elements.

        Personally, I rarely use index, since it's a lot easier to make unintuitive XML - people will naturally assume the parameter order and the element order are the same.

        Hope this helps
        - Don

        Comment

        Working...
        X