1.
Inversion of Control (IoC)
Definition:
Inversion of Control (IoC) means that the control of object creation is transferred from the
programmer to the Spring container.
Why IoC is needed:
• Reduces tight coupling
• Improves maintainability
• Makes testing easier
Example:
Without Spring, objects are created using new keyword.
With Spring, objects are created and managed by the container.
2. Dependency Injection (DI)
Definition:
Dependency Injection is a design pattern where dependencies are provided to a class by Spring
instead of creating them manually.
Types:
• Constructor Injection (Recommended)
• Setter Injection
• Field Injection
Example:
@Service
public class StudentService {
@Autowired
private StudentRepository repository;
}
3. Spring Beans
Definition:
A Spring Bean is an object that is created, managed, and destroyed by the Spring container.
Features:
• Managed by Spring container
• Reusable components
• Lifecycle controlled by Spring
Example:
@Component
public class EmailService {
}
4. Spring Container (ApplicationContext)
Definition:
The Spring Container is responsible for managing beans. ApplicationContext is the core container
interface.
Responsibilities:
• Create beans
• Inject dependencies
• Manage lifecycle
Example:
ApplicationContext automatically detects annotated classes.
5. Annotation-Based Configuration
Definition:
Spring uses annotations instead of XML for configuration, making applications simpler and cleaner.
Common Annotations:
• @Configuration
• @Bean
• @ComponentScan
Example:
@Configuration
@ComponentScan("[Link]")
public class AppConfig {
}
6. Spring MVC (Basic)
Definition:
Spring MVC is a web framework that follows the Model-View-Controller design pattern.
Components:
• Controller
• Model
• View
Example:
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello World";
}
}
7. Exception Handling in Spring
Definition:
Spring provides annotations to handle exceptions in a centralized manner.
Benefits:
• Cleaner code
• Centralized error handling
• Better response management
Example:
@ExceptionHandler([Link])
public String handleException() {
return "Error occurred";
}
8. Why Spring Boot
Definition:
Spring Boot is built on top of Spring Framework to reduce configuration and setup effort.
Advantages:
• Auto-configuration
• Starter dependencies
• Embedded server
Example:
spring-boot-starter-web configures MVC and Tomcat automatically.