Results 1 to 9 of 9

Thread: Unable to inject a bean from child class to parent class

  1. #1

    Default 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. #2
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    286

    Default

    Hi Ganapathi,

    Can you please post your code/configurations?
    Amila Domingo

  3. #3

    Default

    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.

  4. #4
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    286

    Default

    Hi,

    Your configuration should be like this,

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

  5. #5
    Join Date
    Mar 2007
    Posts
    515

    Default

    Quote 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>

  6. #6

    Default

    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

  7. #7
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    286

    Default

    Hi,

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

  8. #8
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

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

  9. #9

    Default

    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.
    Want to see the hogan online news? come to my blog hogan shop.

Posting Permissions

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