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
.
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.
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.
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.
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.
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.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With