Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we are using interface in spring MVC pattern?

I am new to spring MVC , I have downloaded a small spring MVC project . The project is executing fine but it this project interfaces and classes are being used . like


public interface EmployeeService {

    public void addEmployee(Employee employee);

    public List listEmployeess();

    public Employee getEmployee(int empid);

    public void deleteEmployee(Employee employee);
}

And


public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void addEmployee(Employee employee) {
        employeeDao.addEmployee(employee);
    }

    public List listEmployeess() {
        return employeeDao.listEmployeess();
    }

    public Employee getEmployee(int empid) {
        return employeeDao.getEmployee(empid);
    }

    public void deleteEmployee(Employee employee) {
        employeeDao.deleteEmployee(employee);
    }

}

My doubt is if we are using EmployeeServiceImpl what is the need of implementing EmployeeService ? same thing is there in EmployeeDao and EmployeeDaoImpl.

like image 437
som Avatar asked Dec 15 '14 18:12

som


People also ask

What is the role of the model interface in Spring MVC?

It defines a placeholder for model attributes and is primarily designed for adding attributes to the model. It is also used to transfer data between the view and controller of the Spring MVC application. Model interface is available in the org. springframework.

WHAT IS interface and its benefits?

One of the key benefits of using an interface in Java is Testing, the interface makes unit testing easier. The best example of this is DAO pattern and parser classes. If your code is using interface for dependencies then you can easily replace them with a mock, stub, or a dummy for testing.

What is controller interface in Spring MVC?

The Controller interface is explicitly designed to operate on HttpServletRequest and HttpServletResponse objects, just like an HttpServlet. It does not aim to decouple itself from the Servlet API, in contrast to, for example, WebWork, JSF or Tapestry.

What are the main advantages of using interfaces when designing business services in Spring?

As changes in one layer does not effect other layer and new functionalities are made available to other layer immediately. Thus using interface gives you more power over extending and maintaining your application, utilize abstraction and implement good software development practices.


2 Answers

Interfaces are always a good practice for decoupling, but also, when speaking about Spring, there are several features you can use having interfaces rather than concrete classes.

A big advantage is proxying - Spring AOP.

You can find more information here: http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/aop.html

There are other advantages, like post processing and things like that, but I think you will have an interesting reading on Spring AOP.

like image 80
rfceron Avatar answered Oct 17 '22 00:10

rfceron


Weather spring mvc or not, one should always code to interface. Interface gives me better readability when i just want to see what the class does instead of worrying about how it does it, kind of API exposed to outer world.

Another benefit is there could be multiple implementations of 'how to do' it and spring helps to switch easily between multiple implementations. For e.g. you could have one more implementation of EmployeeService say FullTimeEmployeeServiceImpl, RemoteEmployeeServiceImpl.

Now if you have client class which uses EmployeeService:

class EmployeeManager{
    private EmployeeService service;
}

you can inject any of bean here

<bean id="employeeManager" class="com.abc.EmployeeManager">
  <property name="service" ref="fullTimeEmployee | remoteEmployee" >
</bean>

<bean id="fullTimeEmployee" class="com.abc.FullTimeEmployeeServiceImpl" />
<bean id="remoteEmployee" class="com.abc.RemoteEmployeeServiceImpl" />
like image 20
Pranalee Avatar answered Oct 17 '22 01:10

Pranalee