3

I am new for spring framework. I have searched many website for spring mvc on google and i have noticed that created interface for every service and dao but i have some queries as below :

1) What is the main purpose to create interface for every service and dao?

2) How should be project structure for spring mvc?

Parth Soni
  • 11,158
  • 4
  • 32
  • 54
Himanshu Patel
  • 213
  • 2
  • 13
  • Possible duplicate of [what reasons are there to use interfaces (Java EE or Spring and JPA)](https://stackoverflow.com/questions/8894883/what-reasons-are-there-to-use-interfaces-java-ee-or-spring-and-jpa) – Gewure Oct 05 '18 at 16:53
  • Interfaces offer a contract only while Classes offer an implementation. in general you should prefer using interfaces over abstract classes over objects. – Gewure Oct 05 '18 at 16:55
  • 1
    Thank you Gewure for reply, Actually i did not get my answer through tagged duplicate question by you. You can see my confusion in answer's comment box. – Himanshu Patel Oct 06 '18 at 06:18
  • You can also have a look at : [Why separation of interface and implementation?](https://stackoverflow.com/questions/30534524/why-separation-of-interface-and-implementation) – Guillaume Husta Jan 12 '22 at 09:06
  • Or look at this one : [Why would a programmer want to separate implementation from interface?](https://softwareengineering.stackexchange.com/questions/142192/why-would-a-programmer-want-to-separate-implementation-from-interface) – Guillaume Husta Jan 12 '22 at 09:07

1 Answers1

2
  1. What is the purpose of interface

Short answer: dependency injection

Long answer: You see, we do not want concrete implementation and strong coupling in our application. Interfaces serves that purpose, with or without Spring. Spring is a framework that heavily leverages on that. With an interface, you can write multiple implementations of a single logic by defining a contract (interface methods) that describes what is the parameter and return types but you did not specify how is it done. Which gives you a lot of flexibility in writing different Spring beans (Impl classes) that do that. List in Java is an interface, then you have implementations like LinkedList and ArrayList

  1. How should Spring MVC project be structured

Short answer: anyway you like

Long answer: Are you using Spring MVC as API server or serving views like JSP/Thymeleaf? If I am writing an API I would have Controllers (entry point and spring specific features), Facades (business logic that is pure Java without framework classes), and DAO/Services (depending if data comes from database or 3rd party API, maybe both) at bare minimum. For MVC I would have almost similar setups but depending on your agreement with your API provider I might scrap the service layer and focuses more on the Javascript side. In this day and age I'd advise against using JSP/Freemarker. They are a lot slower to develop compared to having let's say React + API server in any language.

Tan Kim Loong
  • 980
  • 7
  • 14
  • 2
    Thank you Tan Kim Loong for reply. But i am still confused regarding some points like we can also achieve dependency injection by auto wiring directly implementation class so why we need interface for dependency injection? This is good thing to define a contract (interface methods) if we have common methods among multiple services but what if i have different methods among multiple service. – Himanshu Patel Oct 06 '18 at 06:11
  • 2
    Using an interface is generally regarded as good practice. Imagine using a non-DI framework. Spring makes it easy by letting you inject any beans you want, even if it's an implementation but remember that layers like your Facade (business logic layer) should not be bound by framework specific annotations for portability purposes. If you use `@inject` instead of `@autowired` then you can port your business logic from Spring to some other framework with 0 code change. Handy =] – Tan Kim Loong Oct 06 '18 at 16:46
  • Thank you so much Tan Kim Loong for answer. Now i am clear and your answer is very useful. – Himanshu Patel Oct 07 '18 at 05:41