-1

I saw a tutorial of spring but I have some doubts about it

If I had a interface like this

package com.journaldev.spring.service;

    import java.util.List;

    import com.journaldev.spring.model.Person;

    public interface PersonService {

        public void addPerson(Person p);
        public void updatePerson(Person p);
        public List<Person> listPersons();
        public Person getPersonById(int id);
        public void removePerson(int id);

    }

and a class which implements the interface

   package com.journaldev.spring.service;

    import java.util.List;

    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import com.journaldev.spring.dao.PersonDAO;
    import com.journaldev.spring.model.Person;

    @Service
    public class PersonServiceImpl implements PersonService {

        private PersonDAO personDAO;

        public void setPersonDAO(PersonDAO personDAO) {
            this.personDAO = personDAO;
        }

        .
        .
        .
    }

and the controller which use the service

package com.journaldev.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.journaldev.spring.model.Person;
import com.journaldev.spring.service.PersonService;

@Controller
public class PersonController {

    private PersonService personService;

    @Autowired(required=true)
    @Qualifier(value="personService")
    public void setPersonService(PersonService ps){
        this.personService = ps;
    }
    .
    .
    .
}

Why the controller has a PersonService object (that in an interface) instead of an PersonServiceIml(class which implements the interface) object????

AFS
  • 1,433
  • 6
  • 28
  • 52

2 Answers2

1

The idea is that designing to interfaces is good practice : What does "program to interfaces, not implementations" mean?

It makes creating a new implementation easy and refactoring is simpler. An interface also ensures mocking is straightforward.

In practice you can get rid of the interface, mockito/powermock etc handle simple classes fine, and in lots of cases you won't need a new implementation or any refactoring.

Community
  • 1
  • 1
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
0

Not sure whether the question is,

  1. Why to use interface or
  2. How the methods are called of the implemented class.

Assuming that, you want to know how the implemented methods are called, please check your dependency injection file. You will see that you have injected your implemented class.

Say for example, it might look something like this,

<bean id="personService" class="com.somethimg.service.impl. PersonServiceImpl">
    </bean> 
Srivatsa N
  • 2,291
  • 4
  • 21
  • 36