Introduction to the Spring Framework
The Spring Framework is a comprehensive Java framework whose core idea is inversion of control (IoC) through dependency injection (DI): instead of your objects creating their own dependencies, a central container creates and wires them for you. This single principle removes a huge amount of boilerplate, makes code dramatically easier to test, and is the foundation everything else in Spring — including Spring Boot, Spring Data, and Spring Security — is built upon.
If you only learn one concept from Spring, make it dependency injection.
The problem Spring solves
Consider a service that needs a repository. Without Spring, the service creates its own dependency:
public class CourseService {
// The service is responsible for building its own dependency — tight coupling
private final CourseRepository repo = new CourseRepository();
}
This is rigid: you cannot swap the repository for a test double, and if the repository's constructor changes, this class breaks. Spring flips the responsibility — hence "inversion of control." The container builds the repository and hands it to the service.
Beans and the container
In Spring, the objects the container manages are called beans. The container (the ApplicationContext) reads your configuration, creates each bean once, wires their dependencies together, and manages their lifecycle. You declare a class as a bean with a stereotype annotation like @Component, @Service, or @Repository, and you mark where dependencies should be injected.
Dependency injection in practice
The cleanest form is constructor injection — Spring sees the constructor parameter and supplies the matching bean automatically:
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
// Marks this as a data-access bean the container will manage
@Repository
public class CourseRepository {
public String findTitle(long id) {
return "Advanced Java"; // simplified for the example
}
}
// Marks this as a service bean
@Service
public class CourseService {
private final CourseRepository repo;
// Spring sees this constructor and INJECTS the CourseRepository bean.
// No "new CourseRepository()" anywhere — the container wires it.
public CourseService(CourseRepository repo) {
this.repo = repo;
}
public String getTitle(long id) {
return repo.findTitle(id);
}
}
Now CourseService does not know or care how its repository is built. In a test you can pass a fake repository to the constructor directly. In production Spring supplies the real one. That flexibility is the whole point.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesWhy constructor injection is preferred
Spring supports field and setter injection too, but constructor injection is the recommended default because:
- Dependencies are final and guaranteed to be set — no half-initialised objects.
- The class clearly declares what it needs in one place.
- It works without Spring at all, which makes unit testing trivial.
More than just DI
Beyond DI, the Spring Framework provides modules for web MVC, data access, transaction management, security, messaging, and more — all designed to work together through the same bean container. Spring Boot (covered separately) layers auto-configuration and sensible defaults on top so you can start building immediately.
Common mistakes
- Calling
newon a dependency instead of injecting it. Creating beans manually bypasses the container and breaks DI's benefits. - Preferring field injection (
@Autowiredon a field). It hides dependencies and makes testing harder; use constructor injection. - Forgetting the stereotype annotation. A class without
@Component/@Service/@Repository(and not otherwise declared) is invisible to the container. - Putting beans outside the scanned packages. Spring only finds beans within its component-scan path; misplaced classes silently never load.
- Confusing the framework with the language. Spring is a library you add; it does not change Java itself.
FAQ
Is the Spring Framework the same as Spring Boot? No. Spring Boot is built on the Spring Framework and adds auto-configuration and an embedded server. See the dedicated comparison for details.
Do I need to learn the Framework before Spring Boot? You should understand DI and beans, which come from the core Framework. Most beginners learn the concepts here and then use Spring Boot to apply them quickly.
Keep learning
Spring's DI powers Spring Boot, and the differences are covered in Spring vs Spring Boot. Explore everything in the Advanced Java hub.
Want to learn Spring the right way, from first principles? Join the waitlist for the Advanced Java course at Infoplanet, Jalgaon.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Advanced Java / Backend Roadmap
A clear, ordered roadmap for learning Advanced Java and backend development — from core Java through servlets, Spring Boot, REST APIs, and deployment.
Deploying a Java Web App
A practical guide to deploying a Spring Boot app: build an executable JAR, run it on a server, containerise with Docker, and check production essentials.
Hibernate & JPA Basics
Hibernate is the leading JPA implementation that maps Java objects to database tables. This guide explains ORM, entities, and the Jakarta Persistence annotations.
