Announcement Announcement Module
Collapse
No announcement yet.
Unable to inject a bean from child class to parent class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to inject a bean from child class to parent class

    Posts: 1

    posted Today 1:41:12 PM private message
    Quote
    Hi All,

    I facing a problem when calling a parent class method test() from child class. This test() method referring another one bean like "testService" which is injecting only when parent class bean configuring. This test() method working fine when work with parent class. The problem is, when i call this same test() method from child class directly, facing NullPointerException on "testService" reference. How to inject this "testService" from child class to parent class when calling parent class method.Here, My Parent class is a not an abstract class. Please advise if anybody knows solution for it. Thanks in advance.

    Note:
    ====

    I tried with Parent attribute and have injected all the beans whatever parent class having. Though not able inject.

    Regards,
    Ganapathi

  • #2
    Hi Ganapathi,

    Can you please post your code/configurations?

    Comment


    • #3
      Please refer the below code snippet for your reference,


      1. Parent Class

      Public class ParentA
      {
      test.EmployeeService employeeService;
      public void setTestService(EmployeeService employeeService){
      this.employeeService = employeeService;
      }
      public EmployeeVO getEmployee(long empId){
      // Business logic here
      EmployeeVO emp = employeeService.getEmployeeServiceBusiness(empId);
      return emp;
      }
      }

      2. Child Class

      Public class ChildB extends ParentA
      {
      public void retriveEmployeeDetails(){
      try{
      EmployeeVO emp = getEmploye(1000);
      }catch(Exception e){
      e.printStakctrace();
      }
      }
      }

      3. Bean Configuration

      <bean id="parentA" class="ParentA" abstract="true">
      <property name="employeeService" ref="employeeService"/>
      </bean>

      <bean id="childb" class="ChildB" parent="ParentA">
      </bean>

      <bean id="employeeService" class="test.EmployeeService">
      </bean>

      4. When i do junit the child class, i am getting exception in when call business method

      Regards,
      Ganapathi.

      Comment


      • #4
        Hi,

        Your configuration should be like this,

        Code:
        <bean id="parentA" class="ParentA" abstract="true">	
               <property name="testService" ref="employeeService"/>	
        </bean>

        Comment


        • #5
          Originally posted by ganapathisundaram View Post
          Please refer the below code snippet for your reference,

          <bean id="parentA" class="ParentA" abstract="true">
          <property name="employeeService" ref="employeeService"/>
          </bean>
          Watch for the property name, the rule follows the naming of the "set*" method:
          Code:
          <bean id="parentA" class="ParentA" abstract="true">
             <property name="testService" ref="employeeService" />
          </bean>

          Comment


          • #6
            Hi All,

            Thanks for the quick reply. Apologize for the typo error.. setter method should be as below mentioned and am using the same way,

            public void setEmployeeService(EmployeeService employeeService){
            this.employeeService = employeeService;
            }

            As well, I am using the bean configuration as you suggested above. Though, still i am facing NullPointerException there. I do not know where committed mistake. It would be so help full if you advise to resolve it.

            Regards,
            Ganapathi

            Comment


            • #7
              Hi,

              Can you please post the stack trace, because i think the wiring happens correctly.

              Comment


              • #8
                He got a resolution elsewhere. He posted in multiple forums/websites. But he got it working now.

                Comment


                • #9
                  Very perfect your post is! I just read articles of other's before, and this time I am pondering your words seriously. It is my appreciation to learn from you something.

                  Comment

                  Working...
                  X