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
3
u/dns1211 Aug 24 '24 edited Aug 24 '24
Using an interface for each services is considered a good practice. it helps with testing, maintainance, extensibility.
For example when using dependency injection and testing. If your controller depends on an interface, you could easily implement a FailService to testing purpose.
Keep in mind that your code base likely grow over the time, so if it’s a project at work, always follow the standards for the sake of your colleagues.