Week 1 Day 1 — What Problem Does Spring Solve?
Goal
Today I want to understand the basic mental model of Spring.
Main questions:
- What problem does Spring solve?
- What is IoC?
- What is Dependency Injection?
- What is a Spring bean?
- What is
ApplicationContext? - What is the difference between Spring Framework and Spring Boot?
1. What Problem Does Spring Solve?
In a normal Java application, classes often create their own dependencies manually.
Example:
public class OrderService {
private final OrderRepository orderRepository;
public OrderService() {
this.orderRepository = new OrderRepository();
}
}
This works, but it creates problems.
Problems
OrderServiceis tightly coupled toOrderRepository.- It is harder to test.
- It is harder to replace
OrderRepositorywith another implementation. - Object creation becomes messy in large applications.
- The class controls too much by itself.
Spring solves this by creating and connecting objects for us.
2. What Is IoC?
IoC means Inversion of Control.
Before Spring:
My code controls object creation.
With Spring:
Spring controls object creation.
Simple definition:
IoC means the control of object creation and dependency management is moved from application code to the Spring container.
Short memory sentence:
IoC means Spring creates and manages objects for us.
3. What Is Dependency Injection?
Dependency Injection means a class receives its dependencies from outside instead of creating them itself.
Bad version:
public class OrderService {
private final OrderRepository orderRepository;
public OrderService() {
this.orderRepository = new OrderRepository();
}
}
Better version:
public class OrderService {
private final OrderRepository orderRepository;
public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
}
Now OrderService does not create OrderRepository.
It receives it from outside.
This is Dependency Injection.
4. IoC vs Dependency Injection
IoC and Dependency Injection are related, but they are not exactly the same.
IoC
IoC is the big principle.
It means the framework controls object creation and object management.
Dependency Injection
Dependency Injection is one way to implement IoC.
It means dependencies are provided from outside.
Memory sentence:
IoC is the idea. Dependency Injection is the technique.
5. What Is a Spring Bean?
A Spring bean is an object managed by Spring.
Example:
@Service
public class OrderService {
}
If Spring creates and manages this object, it is a Spring bean.
But this object is not automatically a Spring bean:
OrderService orderService = new OrderService();
If I create an object manually with new, Spring does not automatically manage it.
Exam definition:
A Spring bean is an object whose lifecycle is managed by the Spring IoC container.
6. What Does “Managed by Spring” Mean?
Managed by Spring means Spring can:
- create the object
- inject dependencies into it
- configure it
- call lifecycle methods
- wrap it with proxies
- destroy it when the application shuts down
Example:
@Service
public class OrderService {
private final OrderRepository orderRepository;
public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
}
Spring creates OrderService.
Spring also injects OrderRepository.
7. What Is ApplicationContext?
ApplicationContext is the main Spring container.
It manages Spring beans.
It knows:
I have an OrderService bean.
I have an OrderRepository bean.
OrderService needs OrderRepository.
I will connect them.
Simple definition:
ApplicationContextis the main Spring container that creates, wires, configures, and manages beans.
8. Two Main Ways to Register Beans
Way 1 — Annotation on a class
@Component
@Service
@Repository
@Controller
@RestController
Example:
@Service
public class OrderService {
}
Spring finds this class through component scanning.
Way 2 — @Bean method
@Configuration
public class AppConfig {
@Bean
public Clock clock() {
return Clock.systemUTC();
}
}
Use @Bean when:
- the class comes from a third-party library
- I cannot add
@Component - object creation needs custom logic
9. What Happens When Spring Boot Starts?
Simplified startup flow:
1. main() method runs
2. SpringApplication.run(...) starts
3. ApplicationContext is created
4. Spring scans packages
5. Bean definitions are registered
6. Beans are created
7. Dependencies are injected
8. Lifecycle callbacks run
9. Embedded web server starts if it is a web app
10. Application is ready
Example:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
10. Spring Framework vs Spring Boot
Spring Framework
Spring Framework provides the core features:
- IoC container
- Dependency Injection
- AOP
- transactions
- MVC
- data access support
Spring Boot
Spring Boot makes Spring easier to configure and run.
Spring Boot provides:
- auto-configuration
- starter dependencies
- embedded Tomcat
- easier configuration
- Actuator
- production-ready defaults
Memory sentence:
Spring is the foundation. Spring Boot makes Spring easier to use.
Practice Questions and Answers
Question 1
What is the problem with this code?
public class OrderService {
private final OrderRepository orderRepository;
public OrderService() {
this.orderRepository = new OrderRepository();
}
}
Answer:
The problem is that OrderService creates OrderRepository by itself. This makes the class tightly coupled to a concrete implementation. It is harder to test, harder to replace with another repository, and harder to let Spring manage dependencies.
Question 2
Why is this better?
public class OrderService {
private final OrderRepository orderRepository;
public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
}
Answer:
This is better because OrderService receives OrderRepository from outside. The dependency is explicit. We can pass a real repository in production and a mock repository in tests. This is Dependency Injection.
Question 3
What is the difference between IoC and Dependency Injection?
Answer:
IoC is the general principle where control is moved from our code to the framework or container. Dependency Injection is a specific technique where dependencies are provided from outside. In Spring, Dependency Injection is the main way IoC is implemented.
Question 4
Is this object automatically managed by Spring?
UserService userService = new UserService();
Answer:
No. If I create an object manually with new, Spring does not automatically manage it. For Spring to manage it, it must be registered as a bean, for example with @Component, @Service, or @Bean.
Question 5
What is a Spring bean?
Answer:
A Spring bean is an object created and managed by the Spring IoC container. Spring can create it, inject dependencies into it, manage its lifecycle, and apply extra behavior such as transactions or security proxies.
Final Memory Sentences
- IoC means Spring controls object creation.
- Dependency Injection means dependencies are provided from outside.
- IoC is the idea. Dependency Injection is the technique.
- A Spring bean is an object managed by Spring.
ApplicationContextis the main Spring container.- Spring Framework provides the core features.
- Spring Boot makes Spring easier to configure and run.