Home > Mobile >  do beans (<beans> defined in XML File) work with only setters and getters in spring?
do beans (<beans> defined in XML File) work with only setters and getters in spring?

Time:12-20

<bean  name="ExampleBean1">
       <property name="name">
           <value>Eren Yaeger</value>
       </property>
</bean>

i defined the above bean in a xml file.

    //   ***   SETTER METHODS   ***
    
    public void setName(String name)
    {
        System.out.println("Setter Method");
        this.name=name;
    }
    
    public void setAddress(String address)
    {
        System.out.println("Setter Method");
        this.address=address;
    }
    
    //   ***   GETTER METHODS   ***
    
    public String getName()
    {
        return name;
    }
    
    public String getAddress()
    {
        return address;
    }
    
    //   ***   CONSTRUCTORS   ***
    
//  public Person(String name,String address)
//  {
//      this.name=name;
//      this.address=address;
//  }

as you can see in above code i commented the constructor but it still getting values so it means that beans only require setter and getter methods?

please try to explain it with reason because i'm completely new to spring.Thanks.

code snippet is above

CodePudding user response:

Because you're using the setting construct And java generates a parameterless constructor for all classes by default, if you don't define a constructor for that class I guess you want to use constructor construction Here is an example of a parameter constructor configuration bean

<bean id="exampleBean"  >
  <constructor-arg value="Eren Yaeger"/>
  <constructor-arg value="Some Address"/>
</bean>
  • Related