Search code examples
javaxmlspringcode-injectionsetter

setter injection in XML not working when assigning values


I am practicing Spring and I created 2 objects(Captain, BlackWidow) for which i defined values using property name and value in my xml file as per my knowledge the name attribute will call the setName(String name) methods on both classes and will pass the values Natasha and Captain to the setName methods. However, when i call these methods, they don't seem to return any value. here is my code:

Captain class:

package com.tutorialspoit;

public class Captain {

private String name;

      public Captain() {
          System.out.println("Captain constructor");
         }

      public String getName() {
          return this.name;
         }

      public void setName(String name) {
       this.name=name;
        }
   }

Avengers class, from which i call the getName() methods from the Captain class:

package com.tutorialspoit;

public class Avengers {

    private Captain steve;
    private BlackWidow natasha;

    public Avengers() {
        System.out.println("Avengers constructor");

    }
    public void setCaptain(Captain cap){
        this.steve=cap;
        }
    public String getCaptain() {
        return steve.getName();
    }

    public void setBlackWidow(BlackWidow bw){
        this.natasha=bw;
        }
    public String getBlackWidow() {
        return natasha.getName();
    }

}

then, here it is my Avengers class:

import org.springframework.context.support.*;

public class MainAvengers {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new 
                ClassPathXmlApplicationContext("applicationContext.xml");
        Avengers avg = context.getBean("myAssembly", Avengers.class);
        avg.getCaptain();
        avg.getBlackWidow();
        context.close();
    }
}

and finally my applicationContext.xml file:

   <bean id="myAssembly" class="com.tutorialspoit.Avengers">
    <property name="captain" ref="steve_rogers"/>
    <property name="blackWidow" ref="natasha_romanov"/>

    </bean>
    <bean id="natasha_romanov" class="com.tutorialspoit.BlackWidow">
    <property name="name" value="Natasha"/>

    </bean>
    <bean id="steve_rogers" class="com.tutorialspoit.Captain">
    <property name="name" value="Captain"/>
    </bean>

when I compile it, it compiles fine, but it doesn't show me the name that i defined in my xml file:

enter image description here

so, my question is, am I assigning the name of my objects correctly? why are the objects are being created but names are not being assigned to them?

any help is much appreciated.

Thanks,


Solution

  • I believe that your code is correct , you're just didnt making the right thing that you want in main :

    public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new 
            ClassPathXmlApplicationContext("applicationContext.xml");
    Avengers avg = context.getBean("myAssembly", Avengers.class); // 1
    avg.getCaptain(); //2
    avg.getBlackWidow(); //3
    context.close();
    

    }

    In 1,2,3 you're just calling the objects , which calls only the default constructor , that's why you're getting only as output Class contructor just like your print in the default constructor method

    So if you want the names , you have to simply print the getters