Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to inject a bean from child class to parent class

 
ganapathi sundaram
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to post your configuration for us to help you with this at all.
 
ganapathi sundaram
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many Thanks Gorder. Its working fine now after correcting all the error as you mentioned. will try to refrain cross-post.

Regards,
Ganapathi.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic