#Should every Spring service have an interface?

1 messages · Page 1 of 1 (latest)

fast echo
#

What do we think about the common FooService + FooServiceImpl pattern in Spring/Java?

I understand why interfaces are useful when they represent a real abstraction: multiple implementations, a plugin/strategy pattern, a public API contract, ports/adapters, or a genuine testing seam…

But when there is only one implementation with no realistic alternative, AND no backwards-compatibility requirement, creating an interface purely so the implementation can be called FooServiceImpl feels like cargo-cult abstraction.

In modern Spring projects, where we can inject, proxy, and test concrete classes, should we default to a concrete @Service class and only introduce an interface when there is a clear reason?

Just to be clear: Im not against interfaces, I’m against interfaces that do not represent a real abstraction

maiden karmaBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
What do we think about the common FooService + FooServiceImpl pattern in Spring/Java? I understand why interfaces are useful when they represent a real abstraction: multiple implementations, a plugin/strategy pattern, a public API contract, ports/adapters

It’s a common debate in Spring whether every service needs an interface. The FooService + FooServiceImpl pattern comes from traditional Java best practices promoting abstraction and flexibility. However, in many Spring projects, especially smaller or internal ones, this can be over-engineering:

  • When interfaces make sense:

    • Multiple implementations (e.g., different behaviors, mocks for testing)
    • Clear API boundaries
    • Plugin or extension points
  • When they might be unnecessary:

    • Only one implementation exists and unlikely to have more
    • Adds verbosity without clear benefit
    • Spring's proxying and AOP work directly on classes now, so interfaces are not mandatory

Bottom line: Use interfaces when you genuinely need abstraction or flexibility. Otherwise, just a concrete service class is often simpler and cleaner. Emphasize pragmatic design, not rigid patterns.


Useful links: