my dog learned polymorphism*
The moose likes Spring and the fly likes Unable to inject a bean from child class to parent class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Unable to inject a bean from child class to parent class" Watch "Unable to inject a bean from child class to parent class" New topic
Author

Unable to inject a bean from child class to parent class

ganapathi sundaram
Greenhorn

Joined: Feb 09, 2010
Posts: 14
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 an interface. Please advise if anybody knows solution for it. Thanks in advance.

Regards,
Ganapathi
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

You will need to post your configuration for us to help you with this at all.


[How To Ask Questions][Read before you PM me]
ganapathi sundaram
Greenhorn

Joined: Feb 09, 2010
Posts: 14
Hi Gorder,

Please refer the below code snippet for your reference,


1. Parent Class

Public class ParentA
{
test.EmployeeService employeeService;
public void setEmployeeService(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">
<property name="employeeService" ref="employeeService"/>
</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.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Well there are lots of problems with this configuration.

for one you class in your bean definitions are not fully qualifying the package. second your parent reference does not match the bean id (they are case sensitive ParentA != parentA)

While what you have will work abstract=true is generally used when you are only using the parent as a template bean or you want to explicitly tell Spring not to try to instantiate the parent. In you case the parent is not abstract so I am not sure that is what you want.

Also wiring the service into the parent and the child is useless as they are the same reference. This would only make sense if you were overriding what was set in the parent with a different implementation.

*Note my example was basic for illustration purposes only code to interfaces not concrete classes

Consider the following configuration


Would work with a test case like this



ClassB



ClassA





Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Also please don't cross post
http://forum.springsource.org/showthread.php?127993-Unable-to-inject-a-bean-from-child-class-to-parent-class

This will have multiple people spending their time explaining the same thing. Pick one place post it and if you don't get any responses in a reasonable period of time then try another.
ganapathi sundaram
Greenhorn

Joined: Feb 09, 2010
Posts: 14
Many Thanks Gorder. Its working fine now after correcting all the error as you mentioned. will try to refrain cross-post.

Regards,
Ganapathi.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to inject a bean from child class to parent class