Q:

Getter Method retuning null value in Spring DI

I am trying to test Spring Dependency Injection functionality but running into issues when trying to retrieve value of an instance variable using getter methods of Spring Bean class. Getting null value of a previously set instance variable. 
My Main Class
public class Main {
public static void main(String ... args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("BeanFactory.xml");
    IBackup dataWriterObj = (IBackup) context.getBean("writeBackupHDFS");
    backup backup = new backup();
   backup.setNickName("Abbey");
    String result = dataWriterObj.read("Ankur", "Bahre");
System.out.println(result);
}
}

Bean Class
public class backup implements IBackup {
private String nickName;

public String getNickName() {
    return nickName;
}

public void setNickName(String nickName) {
    this.nickName = nickName;
}

public String read(String firstName, String lastName) {
    String Name = "My Name is " + firstName + " " + lastName + " and my nickName is "  + getNickName();
    return Name;
}
}

Interface
public interface IBackup {

public String read(String firstName, String lastName);
}

BeanFactory.xml 
<bean id="writeBackupHDFS" class="test.backup"/>

Expected Output- My Name is Ankur Bahre and my nickName is Abbey
Actual Output- My Name is Ankur Bahre and my nickName is null
Can anyone suggest what is it that I am missing ?
Thanks

A:

Use dataWriterObj.setNickName("Abbey") instead of backup.setNickName("Abbey").
Because below code has no relation with dataWriterObj object.
   backup backup = new backup();
   backup.setNickName("Abbey");

Edit: 
Create another Class with name
public class Name {
    private String nickName;

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
}

Update backup class with the above change
public class backup implements IBackup {

    @Autowired
    Name nameObj;   

    public void setNameObj(Name nameObj) {
        this.nameObj = nameObj;
    }

    public String read(String firstName, String lastName) {
        String Name = "My Name is " + firstName + " " + lastName + " and my nickName is "  + nameObj.getNickName();
        return Name;
    }
}

Add it to conf file BeanFactory.xml:
 <bean id="writeBackupHDFS" class="springboot.rest.backup">
            <property name="nameObj" ref="nameBean"/>
           </bean>
<bean id="nameBean" class="springboot.rest.Name"/>

Now update main method with below code
    ApplicationContext context = new ClassPathXmlApplicationContext("BeanFactory.xml");
        IBackup dataWriterObj = (IBackup) context.getBean("writeBackupHDFS");
        Name nameBean = (Name) context.getBean("nameBean");
     //   backup backup = new backup();
        nameBean.setNickName("Abbey");
        String result = dataWriterObj.read("Ankur", "Bahre");
    System.out.println(result);

