Zat
Zat

Reputation: 19

How to use @Service class from another Web App Project

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

  1. Java Build Path -> Add Project
  2. Project Reference -> Add Project

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

Answers (1)

sergiopolog
sergiopolog

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:

  1. Annotate the CustomerService implementation (CustomerServiceImpl in ProjectA??) with @Service, for tell Spring to instantiate that implementation.

  2. Add jar of ProjectA in ProjectB

  3. Tell Spring to scan for beans to instantiate, in xml configuration file: <context:component-scan base-package="base.package.in.project.a" />

  4. Annotate customerService property in CustomerController with @Autowired for inject the instance of CustomerServiceImpl in spring context into this property.

Upvotes: 0

Related Questions