r/SpringBoot • u/BluePillOverRedPill • Aug 23 '24
Interface for each service?
Hi Everyone!
I’m a bit confused about the use of interfaces for all services in Spring Boot. I understand that interfaces serve as a contract and offer benefits like easier testing and flexibility for future changes. However, I’ve also read that if you’re not planning to have multiple implementations of a service, using interfaces might be unnecessary. Could you clarify the best practices for using interfaces in this context, and explain how they impact dependency injection and testing?
24
Upvotes
1
u/Big_Sell5559 Aug 24 '24
All depends of the use case and the purpose of your project. If you pretend to build a library for external use, of course you'll have to implement with interfaces all around for providing your users with the needed flexibility. If the Op is asking for the legacy practice of creating interfaces for controllers, services and components in Spring, you'll have to bear in mind that in 2024 with modern Java and Spring is totally superfluous and a bad practice.