DEV Community

Elayaraj C
Elayaraj C

Posted on

🌿 Understanding Spring, Spring Boot, IoC, and DI 🎨 What is MVC?

🌱 What is Spring?

Spring is a popular Java framework that helps you build powerful applications easily.

Imagine you’re building a house (your application). You could do everything yourself β€” mix the cement, lay the bricks, install the pipes β€” or you could use tools and workers to make the job easier.

Spring gives you:
βœ… Tools
βœ… Ready-made components
βœ… A way to organize everything cleanly

With Spring, you can build:

  • Web applications
  • Backend services
  • Cloud apps
  • Microservices

Spring helps manage your app’s pieces (called beans) and how they talk to each other β€” saving you from writing repetitive or messy code.
⚑ What is Spring Boot?

Spring Boot is like Spring on steroids.

Normally, when you use Spring, you have to:

  • Write a lot of configuration
  • Set up servers
  • Connect all the pieces manually

Spring Boot does most of this for you.

With Spring Boot, you get:
βœ… Automatic configuration
βœ… Embedded servers (like Tomcat) β€” just run java -jar
βœ… Faster project setup with starters
βœ… Easy monitoring with tools like Actuator

In short, Spring Boot makes it super easy to start and run Spring applications β€” especially for microservices or web apps.

🧩 What is IoC (Inversion of Control)?

This sounds fancy, but it’s really about who controls what.

Normally, in regular Java:

Car car = new Car();
Enter fullscreen mode Exit fullscreen mode

Your code controls how the Car is created.

With IoC, you let Spring control the creation:

  • You tell Spring: β€œHey, I need a Car.”
  • Spring gives you the ready-made Car.

This inversion (letting the framework handle creation) is called Inversion of Control.

Why is this useful?
βœ… You don’t have to manually manage complex object creation
βœ… It keeps your code clean and flexible

πŸ”— What is DI (Dependency Injection)?

Dependency Injection is how IoC works.

Let’s say your Car needs an Engine.

Without DI:

Engine engine = new Engine();
Car car = new Car(engine);

Enter fullscreen mode Exit fullscreen mode

With DI:


@Component
public class Car {
    @Autowired
    private Engine engine;
}

Enter fullscreen mode Exit fullscreen mode

You don’t create the Engine yourself β€” Spring injects it for you.

Benefits:
βœ… Less coupling (classes don’t tightly depend on each other)
βœ… Easier testing (you can inject mock objects)
βœ… Better maintainability

Image description

🎨 What is MVC?

MVC stands for Model - View - Controller.
It’s a design pattern used to organize code in web applications (and even desktop apps) to make things cleaner, more modular, and easier to maintain.

πŸ— Breakdown of MVC

βœ… Model β†’ the data + business logic
βœ… View β†’ what the user sees (UI)
βœ… Controller β†’ handles user input and connects Model + View

Let’s go one by one.
πŸ“¦ Model

  • Represents the data and rules of your application.
  • It’s where you define things like:

1) What’s a User? What fields does it have?
2) How do you get data from the database?
3) What business rules apply?
Example:

public class User {
private String name;
private String email;
// getters, setters, validation, etc.
}

🎨 View

  • This is the front-end, the part users interact with.
  • In web apps, it’s usually HTML, CSS, maybe with some JavaScript.
  • It shows the data from the model.

Example:

<h1>Welcome, ${user.name}!</h1>

πŸ•Ή Controller

  • The brain that handles user actions.
  • It receives input (like a button click or form submission), talks to the Model, and decides which View to show.

Example:

@Controller
public class UserController {
    @GetMapping("/profile")
    public String getProfile(Model model) {
        User user = userService.getCurrentUser();
        model.addAttribute("user", user);
        return "profile";
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ›  How they work together

1️⃣ User clicks a button or enters a URL β†’
2️⃣ Controller handles the request, calls the Model β†’
3️⃣ Model returns data β†’
4️⃣ Controller passes data to the View β†’
5️⃣ View renders the final HTML to the user

🌟 Why use MVC?
βœ… Separates concerns β†’ keeps code clean
βœ… Easier to maintain β†’ change one part without breaking others
βœ… More testable β†’ you can test logic without UI
βœ… Works well for teams β†’ backend + frontend developers can work separately

πŸš€ In Spring Boot

Spring Boot uses Spring MVC to build web apps.

It automatically connects:

  • Model β†’ your Java classes + services
  • View β†’ templates like Thymeleaf, JSP, or HTML
  • Controller β†’ annotated with @Controller or @RestController

Top comments (0)