Results 1 to 6 of 6

Thread: How to solve Spring IoC during Inheritance

  1. #1
    Join Date
    Jul 2006
    Posts
    20

    Default How to solve Spring IoC during Inheritance

    Hi,


    I have a classs SpringManager, it extends another class BaseManger. I have another class Basemanger.

    Entries in the Spring context file are :

    <bean id="SpringManager" class="com.test.springweb.bus.SpringManager">
    </bean>
    <bean id="BaseSeason" class="com.test.springweb.bus.BaseSeason">
    <property name="test" ref="Test"></property>
    </bean>


    SpringManager has a method check()
    public String check()
    {
    test(); // calls BaseManager's method
    }

    BaseManager has a mehtod test()

    public void test()
    {
    test.testmethod(); // calls testmethod() of Test class
    }


    When run the application and call SpringManager's check method, am getting
    Null pointer exception in test.testmethod() line.

    The corresponding setter methods are proper.


    I guess it is may be cos SpringManager does nto have a refernce to its base class.
    Doesnt Spring creates an object of its base also, thus creating the object of its refrence?


    Can you anyone help regarding this ? Thanks in advance.

  2. #2
    Join Date
    Feb 2006
    Location
    Arlington, VA, USA
    Posts
    194

    Default

    Quote Originally Posted by cse_gokul
    Entries in the Spring context file are :

    <bean id="SpringManager" class="com.test.springweb.bus.SpringManager">
    </bean>
    <bean id="BaseSeason" class="com.test.springweb.bus.BaseSeason">
    <property name="test" ref="Test"></property>
    </bean>
    Where's your bean id=... reference for "Test"? ref="Test" I believe indicates that you have defined the bean Test someplace. Also, does BaseSeason have a setter ready for Test, (i.e., setTest()), so it can be initialized?

    Glen

  3. #3
    Join Date
    Jul 2006
    Posts
    20

    Default How to solve Spring IoC during Inheritance

    Hi

    I have the entry for Test bean also i nthe same context file.

    <bean id="Test" class="com.test.springweb.bus.Test"></bean>

    Also have the setter method for Test in BaseSeason.

    public class BaseSeason
    {
    Test test;

    public void test()
    {
    try
    {
    test.testmethod();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    public void setTest(Test test)
    {
    this.test = test;
    }
    }


    When i run the application, it actually calls the SpringManager class check ()method. From there its base class method test() is called.

    Please help me regarding this.

    Also i want to know when exactly the objects are created for the class, when the context files are loaded or when the class is called at runtime ?

  4. #4
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    What has BaseSeason to do with BaseManager? You assign a "Test" instance to a bean of type "BaseSeason". Afterwards you expect "Test" to be set on another bean of type "SpringManager" (subtype of "BaseManager").

    You need to assign the "Test" bean to the "SpringManager" bean to get things going.

    As of the time of creation (which has no influence on this problem as I see it): When defined as singleton (the default) the beans are instantiated at context creation. Otherwise (or if explicitly marked as lazy-init) they will be instantiated when referred to.

    Regards,
    Andreas

  5. #5
    Join Date
    Jul 2006
    Posts
    20

    Default

    Got it buddy. Thanks a lot.

    I thought the reference should be made only for the class it uses.
    That is why i referenced Test bean for BaseSeaon.
    Though it is the BaseSeaon which actually calls Test class, we can refrenace it via its child Class SpringManger.

    Thus i find that, If Class A extends Class B which extends Class C and Class C is the one which actually calls a Test Class, we can give the reference to Test in Class A in the context file and setter method in the class C which actually calls it.


    Please correct me if am wrong and can you give few details on the bean creation and dependencies in this type of Inheritance.

  6. #6
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    It is ok so far. Just do not confuse classes with beans. If you have a hierarchy as the one you mention (A extends B extends C) then an instance of A has all properties and methods of the whole hierarchy. There are no distinct A, B, C parts of that instance, it is just one.

    Regards,
    Andreas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •