0

New to Spring framework. I am experimenting around with injection. For that I have created some simple test classes.

Basically, I want that the variable num in class2 should be injected with value 10 after method asd() from class1 is executed.

My code is as following:

class1.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class class1 extends class2 {

    public int num;

    public void setMessage(int num) {
        this.num = num;
    }

    public int getMessage() {
        System.out.println("getnum: " + num);
        return num;
    }

    public void asd() {
        num = 10;
        ApplicationContext context = new ClassPathXmlApplicationContext("test/Beans.xml");
        test.class2 login2 = (test.class2) context.getBean("class2");
        login2.disp();

    }
}

class2.java

public class class2 {
    public int num;

    public void setNum(int num) {
        System.out.println("setnum: " + num);
        this.num = num;
    }

    public int getNum() {
        return num;
    }

    public void disp() {
        System.out.println("num: " + num);
    }

}

main.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class main {
    public static void main(String args[]) {

        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

        test.class1 login = (test.class1) context.getBean("class1");
        login.asd();
    }
}

Beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

<bean id="class2" class="test.class2" parent="class1" lazy-init="true" />

<bean id="class1" class="test.class1" />

</beans>

I am expecting the value of num when the method asd() is executed to be displayed as 10. But its giving 0. Can anyone tell me what am I doing wrong? Even tried lazy-initializing it so it initializes after the value is set. But the result is still same...

user3271166
  • 573
  • 1
  • 6
  • 17
  • Why are you expecting it to give `10`? What part of your context will set the value to 10? – Sotirios Delimanolis Oct 28 '15 at 16:18
  • Spring's not invoking that method when it's initializing the bean, so I wouldn't expect it to set the value to anything other than 0. – Makoto Oct 28 '15 at 16:19
  • That's not really what spring is for. Spring is, mostly, used to bring up a set of related singletons that provide services to your application. It's not really meant to be used for application logic, it's more for architectural components. – Software Engineer Oct 28 '15 at 16:22
  • 1
    You have declared `class1` and `class2` each to have a member variable called `num`. That would be very odd, since `class1` also inherits `num` from `class2`. – khelwood Oct 28 '15 at 16:22
  • 1
    Why are you creating so many contexts? – Sotirios Delimanolis Oct 28 '15 at 16:26
  • 2
    You have created two objects, why would you expect invoking a method on one effects member variables of the other when all that connects them is an inheritance relationship? I think you need to think about how you would do what you want outside of Spring (which at the end of the day is only instantiating your objects) and then look at expressing that in Spring – Nick Holt Oct 28 '15 at 16:26
  • I was actually wondering if there is some way other than specifying `init` in spring to execute a method when the bean is initialized? Like I wanted that spring should pass the `num` value after method `asd()` has executed. Is there any way other than using `init`property of spring? Because I want to execute this method on button click. But maybe as @Engineer Dollery said, I am probably using spring wrong... should probably move on by passing value with just simple java. – user3271166 Oct 28 '15 at 16:30

0 Answers0