3- Layer Architecture in Spring


15/04/25

4 mins

🌱 Why I Started With Spring Core Instead of Jumping Into Spring Boot

When I first dipped my toes into the Spring ecosystem, everyone around me was raving about Spring Boot — "It’s fast", "Zero configuration", "Production-ready in minutes". While all that sounded cool, I decided to start a bit old-school: with Spring Core.

And honestly? I’m glad I did.

Let me walk you through why that decision made a difference, and how it helped me understand the why behind Spring Boot instead of just the how.


🧱 The Backbone: 3-Layer Architecture in Spring

Before touching any code, I got introduced to a pattern that most Spring apps follow — the three-layered architecture:

1. Web Layer (Controller Layer)

This is the layer that talks to the outside world — like your frontend or REST clients. It receives the request and delegates the task to the service layer.

2. Service Layer (Business Logic)

This layer does the actual processing — validations, calculations, whatever logic your app needs.

3. Repository Layer (Data Access Layer)

This talks to the database. Earlier, this meant writing a lot of JDBC boilerplate. Now, we use tools like Hibernate and Spring Data JPA that simplify it a lot.


🏗️ From Old School to Modern Tools

Here’s how the landscape evolved:

ConcernOld TechModern Alternative
Request HandlingServlets, JSPSpring MVC
Web FrameworkStrutsNot used much anymore
Database AccessJDBCHibernate + Spring Data

I started by understanding these old patterns (yes, even writing some Servlet code 😅) just to see the pain points — and man, Spring makes life so much easier.


🧩 Spring Modules: What’s What ?

The Spring Framework is modular — each module does one job. Here's a quick summary:

  • Spring Core: Foundation layer. Teaches you concepts like Inversion of Control (IoC) and Dependency Injection (DI).

  • Spring MVC: Helps build REST APIs or web apps.

  • Spring Data JPA: Makes database interaction painless.

Most of the magic in Spring Boot actually comes from these modules — Boot just auto-configures them for you.


🤝 Tight Coupling vs Loose Coupling (Why DI Matters)

Let’s say you’re building a course enrollment app. You might start with something like:

SpringBootCourse course = new SpringBootCourse();

But what if tomorrow you want to switch to a DevOpsCourse? You’d have to go change your class.

That’s tight coupling — your code depends directly on a concrete implementation.

Instead, if you do this:

ICourse course = new SpringBootCourse();

Now your code only depends on the interface — and you can easily switch the implementation. This is loose coupling.


🧪 Here’s a Simple Example

I built a small demo using the above concept. Check this out 👇

Interface:
ICourse.java

public interface ICourse { boolean getTheCourse(double amount); }

Implementations:

public class SpringBootCourse implements ICourse { public boolean getTheCourse(double amount) { return amount >= 3000; } } public class DevOpsCourse implements ICourse { public boolean getTheCourse(double amount) { return amount >= 5000; } }

Consumer Class:
College.java
( Dependancy Injection )

public class College { private ICourse course; public College(ICourse course) { this.course = course; } public void setCourse(ICourse course) { this.course = course; } public boolean buyTheCourse(double amount) { return course.getTheCourse(amount); } }

Main App: ( Inversion of controll )

public class LaunchApp { public static void main(String[] args) { College college = new College(new SpringBootCourse()); // Switch course using setter college.setCourse(new DevOpsCourse()); boolean status = college.buyTheCourse(4545.4); if (status) System.out.println("Enrolled to Course"); else System.out.println("Failed to enroll"); } }

🚀 Takeaway

Learning Spring Core first helped me:

  • Understand why DI is important.

  • Write flexible, testable code.

  • Appreciate the conveniences Spring Boot offers — because I’ve seen what’s underneath the hood.

If you’re starting out, my advice is: don’t skip Spring Core. Just a couple of weeks there, and everything else will make a lot more sense.