Do You Really Need Interfaces for Service and DAO Layers in Spring?

This article examines whether Service and DAO layers in Spring projects truly require interfaces, debunks common justifications, explores alternative project structures for single and multiple implementations, and offers practical guidelines on when to use or omit interfaces.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Do You Really Need Interfaces for Service and DAO Layers in Spring?

Recently I saw the question “Is it necessary to add an interface for every Service and DAO class?” and gave a brief answer: “It depends”.

When using a dependency‑injection framework like Spring, you can omit interfaces.

Why people think interfaces are needed

Allows writing upper‑layer code (e.g., Controller) before lower‑layer logic is implemented.

Spring’s default AOP uses dynamic proxies, which require interfaces.

Enables multiple implementations of a Service.

In practice, these reasons are not convincing.

First reason debunked

Writing code against an interface is useful only when different teams work on separate modules; however most projects are organized by feature, not by layer, so defining an interface for each layer adds unnecessary work.

You can simply write empty methods in the lower layer to satisfy the upper layer.

Second reason debunked

Spring’s default proxy mechanism can be switched to CGLIB, which does not require interfaces.

Third reason debunked

Multiple implementations are rarely needed; when they are, other techniques can replace interfaces.

Typical project structure

Controller

Service

Dao

If multiple implementations are required, you can organize packages like:

Controller

Service (interface package) → impl, impl2

Dao

Two approaches for handling multiple implementations:

Add a new package under Service and configure the new implementation in the DI configuration.

Create a separate Service module with its own package and configure it as the injected bean.

Both approaches increase code size; the second yields clearer module separation.

Combining advantages

Separate the interface and its implementations into an independent module, then adjust packaging so that ServiceImpl and ServiceImpl2 share the same package structure, allowing you to switch between them without changing DI configuration.

If you remove the interface module entirely, you can still have multiple implementations by adjusting module dependencies, but you lose the compile‑time contract and IDE assistance.

Conclusion: Use interfaces only when you truly need multiple implementations; otherwise, they are unnecessary.

Feel free to share your thoughts in the comments.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

springdependency-injectiondaoInterface DesignService Layer
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.