The Problem With Traditional Java
When I first started learning backend development, I kept hearing: “Just use Spring.” But honestly? The concepts sounded terrifying. IoC, DI, Beans — it felt like a wall of jargon I’d never understand.
Then I actually tried building something without a framework. That’s when I got it.
Here’s what building a simple REST API looks like without Spring:
public class EmployeeService {
private EmployeeDAO employeeDAO;
public EmployeeService() {
// Manually creating dependencies
DatabaseConnection dbConn = new DatabaseConnection(
"jdbc:mysql://localhost:3306/mydb",
"root",
"password"
);
this.employeeDAO = new EmployeeDAO(dbConn);
}
}
Problems:
Every class manually creates its dependencies
Can’t test without a real database
Change database credentials? Update 20 files
Objects are tightly coupled — hard to maintain
This is where Spring comes in.
What Actually Is Spring?
Spring Framework is a collection of tools that handles all the repetitive infrastructure code — object creation, configuration, database connections — so you can focus on building features.
Think of it like this:
Without frameworks: Build a house by cutting trees, making bricks, mixing cement
With Spring: Assemble pre-made walls, doors, and plumbing
Spring was created in 2002 by Rod Johnson to simplify enterprise Java development, which was notoriously complex at the time.
The Three Core Concepts (Simplified)
- Inversion of Control (IoC) In simple terms: Instead of YOU creating objects, Spring creates them for you.
Traditional way:
EmployeeService service = new EmployeeService(); // You control creation
Spring way:
@Autowired
private EmployeeService service; // Spring controls creation
- Dependency Injection (DI) In simple terms: Spring automatically passes (injects) the objects your class needs.
@Service
public class EmployeeService {
private final EmployeeDAO dao;
// Spring injects EmployeeDAO here
public EmployeeService(EmployeeDAO dao) {
this.dao = dao;
}
}
No manual object creation. No tight coupling. Easy to test.
- Beans In simple terms: A Bean is just an object that Spring manages.
@Service // This is a Bean
public class EmployeeService { }
@Repository // This is also a Bean
public class EmployeeDAO { }
Spring creates these at startup and injects them wherever needed.
The Magic of Auto-Configuration
The feature that made me fall in love with Spring? Spring Boot’s auto-configuration.
Look at this — a complete web application:
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
This automatically:
Starts a web server
Configures Spring
Scans for components
Sets up defaults
Want a database? Add one dependency and Spring configures it automatically. No XML, no endless setup.
Real Example: Building a REST API
Without Spring (~200 lines of configuration)
You’d manually configure servlets, parse JSON, handle exceptions, manage transactions…
With Spring Boot (~20 lines)
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
@Autowired
private EmployeeService service;
@GetMapping("/{id}")
public Employee getEmployee(@PathVariable int id) {
return service.getEmployee(id);
}
@PostMapping
public Employee createEmployee(@RequestBody Employee employee) {
return service.saveEmployee(employee);
}
}
Spring automatically:
Maps URLs to methods
Converts JSON ↔ Java objects
Handles HTTP status codes
Manages database transactions
Why Testing Is a Game Changer
Here’s what blew my mind when I started writing tests:
@SpringBootTest
class EmployeeServiceTest {
@MockBean
private EmployeeRepository repository;
@Autowired
private EmployeeService service;
@Test
void testGetEmployee() {
// Mock data—no real database needed!
Employee mock = new Employee(1, "John");
when(repository.findById(1)).thenReturn(Optional.of(mock));
Employee result = service.getEmployee(1);
assertEquals("John", result.getName());
}
}
You can test business logic without touching the database. Components are isolated. This is the power of loose coupling.
The Spring Ecosystem
Once you learn Spring basics, you unlock powerful modules:
Module Purpose Spring Boot Rapid setup with auto-configuration Spring Data Database access without writing SQL Spring Security Authentication & authorization Spring Cloud Build microservices (Netflix uses this)
Spring Data Example
Without Spring Data:
String sql = "SELECT * FROM employees WHERE department = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
// ... 20+ lines of boilerplate
With Spring Data:
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
List<Employee> findByDepartment(String department);
}
Spring generates the implementation at runtime. That’s it.
Try It Yourself (5 Minutes)
Go to start.spring.io
Select “Spring Web”
Download and unzip
Create a controller:
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "World") String name) {
return "Hello, " + name + "!";
}
}
Run: mvn spring-boot:run
Visit: http://localhost:8080/hello
You just built a web server with zero configuration.
Should You Learn Spring?
Yes, if:
You’re building Java web applications
You want to become a Java backend developer
You value clean, testable code
You’re job hunting (60% of Java jobs require Spring)
Maybe not, if:
You’re building tiny scripts
You want absolute control over everything
You prefer ultra-lightweight frameworks
How Spring’s Core Concepts Connect
How it flows:
IoC Container scans for @Component, @Service, @Repository annotations
Creates all Beans and stores them in its container
When Controller needs Service, DI injects it automatically
When Service needs DAO, DI injects that too
You just write business logic — Spring handles the wiring
The Bottom Line
Spring isn’t complicated — it’s just unfamiliar. Once you understand:
IoC — Spring creates objects
DI — Spring passes objects where needed
Beans — Objects managed by Spring
…everything else clicks into place.
The framework exists to handle boring infrastructure so you can focus on building features. That’s it.
Resources to Get Started
Spring Boot Official Guides
EmbarkX YouTube Channel
Spring Documentation
Spring Initializr
Got questions about Spring? Drop them below — I’d love to help you get past that initial learning curve.
Lets connect — @thedivyaojha❤️…
Top comments (0)