Reputation: 19
I'm having a problem right now, I have two Dynamic Web App Project (ProjectA and ProjectB) and I need to reuse methods from Service class from ProjectA. Right now I'm developing ProjectB, the methods I need to reuse from ProjectA are pulling datas from its own database, and I need to have those data.
I've already done doing this following steps
and I also tried to add ProjectA jar file as Maven dependency and still not working
In ProjectB, I can already import and call methods from ProjectA but I'm having a nullPointerException.
ProjectA.java
public interface CustomerService {
public Customer getCustomerDetails(Long id);
}
The above class is the class that I need to reuse, for to me have ProjectA data.
ProjectB.java
public class CustomerController {
private CustomerService customerService;//service from another project
public ModelAndView getCustomer(Long id){
Customer = this.customerService.getCustomerDetails(id);
}
}
Now, In ProjectB I need to call CustomerService.class and use its own method for me to have its own data. But unluckily I'm having a NullPointerException everytime call CustomerService interface
Please help I can't get the right thing to do.
Thanks in advance
Upvotes: 0
Views: 1038
Reputation: 31
Your problem is that you are not telling Spring to instantiate an object that implements CustomerService
, for being injected in CustomerController
. You can do:
Annotate the CustomerService
implementation (CustomerServiceImpl
in ProjectA??) with @Service
, for tell Spring to instantiate that implementation.
Add jar of ProjectA in ProjectB
Tell Spring to scan for beans to instantiate, in xml configuration file: <context:component-scan base-package="base.package.in.project.a" />
Annotate customerService
property in CustomerController
with @Autowired
for inject the instance of CustomerServiceImpl
in spring context into this property.
Upvotes: 0