Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I really need to create interfaces in Spring?

In my Spring project I have many simple services to fetching data (just a simple CRUD). The design of the developers that started this project was to create the implementation for each of the service like

public interface UserService

and then implementation like

public class UserServiceImpl implements UserService

Since there is no chance that UserService will have more implementation I'm really sick of these Impl suffix and the more I read (e.g. this article) I'm realising that I have reasons to being sick

I had a discussion with a friend from a team last week and I shared my thoughts with him but what he answered was 'basically you're right but Spring likes interfaces and works with them better than with classes'.

Unfortunately I'm not an expert in Spring and, however I was trying to look for some arguments, I was not able to find an answer was he right.

Are there some strong arguments to use such approach in Spring to have interface for every little service class?

like image 390
keksimus.maximus Avatar asked Mar 10 '19 12:03

keksimus.maximus


People also ask

Should Spring services have interfaces?

So, if you ask me whether you should use an interface for your services, my answer would be no. The only exception is if you're either trying to use inversion of control, or you have multiple implementations to take care of.

Do we really need interfaces?

We need interfaces : To achieve total abstraction. To achieve security. Java doesn't allow multiple inheritance but it can be achieved by implementing multiples interfaces.

Why do we create interface in Spring?

Using Interfaces allows your classes to extend from some other classes if required. Your Interfaces can have multiple implementations and you can switch between any of them without changing the client code.

What are the benefits of using Spring Framework no need for interfaces?

One of the major benefits of Spring Framework for development of enterprise application is that you can leverage from Spring. Spring uses technologies such as JDK timers, ORM frameworks, Java EE etc. So that developers need not have to learn all those technologies or frameworks in order to develop applications.


Video Answer


3 Answers

I can tell from real world projects, that it works well without interfaces only having the implementing class. Following the principle "You aren't gonna need it" (YAGNI), you simplify your code if you follow that rule. Dependency Injection works also well with classes, interfaces are not a requirement for it.

Sure you can write and reuse test implementations, but you can do the same with mocks e.g. with mockito and overwrite the behavior of your implementation class for test cases.

like image 174
Simulant Avatar answered Oct 17 '22 00:10

Simulant


I've gone through all the Answers here, But would like to add more on proxy

AOP can use JDK proxy OR CGlib proxy

If Class has implemented interface it will use JDK proxy(preferred whenever you have a choice). If Class has not implemented interface it will use CGlib proxy.

like image 20
Ravi Parekh Avatar answered Oct 17 '22 00:10

Ravi Parekh


Wherever you want to reap benefits of dependecy injection (DI) pattern you need to program against abstractions, usually an interface.

There are more benefits to DI, but the most persuasive seems to be it allows unit testing. There your interfaces will get at least one more implementation (the mock implementantion), when you will want to test your class in isolation from its dependencies (those production implementations of the interfaces).

That said, that doesn't mean every class must implement some interface. Some parts of code can be tightly coupled together without problem.

Note that using Spring or not doesn't play role in the use DI/not use DI decision.

like image 2
Igand Avatar answered Oct 17 '22 01:10

Igand