What is the Spring IoC Container?
The Spring IoC (Inversion of Control) container is at the core of the Spring framework. It is responsible for managing the lifecycle and dependencies of Spring beans. The container automatically instantiates, configures, and assembles beans based on the configuration provided.
Types of IoC Containers in Spring:
- BeanFactory – The simplest container that provides basic dependency injection features.
- ApplicationContext – A more advanced container that includes features like event propagation, declarative mechanisms, and integration with Spring AOP.
Dependency Injection (DI) and Inversion of Control (IoC)
Inversion of Control (IoC) is a design principle where the control of object creation and dependency management is transferred from the developer to the Spring IoC container.
Dependency Injection (DI) is a technique to achieve IoC. It allows Spring to inject dependencies into a class instead of creating them manually.
Types of Dependency Injection in Spring:
- Constructor Injection
- Setter Injection
- Field Injection (not recommended due to difficulty in testing and dependency management)
Difference Between @Component
, @Service
, and @Repository
Spring provides different stereotype annotations for component scanning:
Annotation | Purpose |
---|---|
@Component |
Generic annotation for any Spring-managed bean. |
@Service |
Specialized annotation for service-layer classes. |
@Repository |
Used for DAO (Data Access Object) classes, provides exception translation. |
@Bean
vs. @Component
Feature | @Bean |
@Component |
---|---|---|
Definition | Declared in a configuration class (@Configuration ) |
Used on classes that are automatically scanned. |
Scope | Manually defined by developer | Auto-detected by Spring during component scanning. |
Use Case | When explicit control over bean creation is needed | When automatic component scanning is sufficient. |
What is Spring AOP (Aspect-Oriented Programming)?
Spring AOP is a programming paradigm that allows separating cross-cutting concerns such as logging, security, and transaction management from business logic.
Key Concepts in AOP:
- Aspect – A module that contains cross-cutting logic.
- Join Point – A specific point in application execution (e.g., method execution).
- Advice – Action taken at a particular join point (e.g., Before, After, Around advice).
- Pointcut – Expression that selects join points.
- Weaving – Process of linking aspects with target objects.
Example of AOP in Spring:
@Aspect
@Component
public class LoggingAspect {
@Before("execution(* com.example.service.*.*(..))")
public void logBefore(JoinPoint joinPoint) {
System.out.println("Method called: " + joinPoint.getSignature());
}
}
Bean Scopes in Spring
Spring provides different bean scopes to manage the lifecycle of beans.
Scope | Description |
---|---|
singleton |
Default scope, only one instance per Spring container. |
prototype |
A new instance is created each time it is requested. |
request |
A new bean instance is created for each HTTP request. |
session |
A new bean instance is created for each HTTP session. |
application |
A single instance for the entire web application. |
How Does Spring Transaction Management Work?
Spring provides a robust transaction management mechanism to handle database operations effectively.
Key Concepts:
-
Declarative Transaction Management – Using
@Transactional
annotation. -
Programmatic Transaction Management – Using
TransactionTemplate
manually.
Example of Declarative Transaction Management:
@Service
public class AccountService {
@Transactional
public void transferMoney(Long fromAccount, Long toAccount, Double amount) {
// Deduct from source account
accountRepository.debit(fromAccount, amount);
// Credit to destination account
accountRepository.credit(toAccount, amount);
}
}
Spring automatically manages transactions using proxies and rollback strategies based on exceptions.
This blog post covers the fundamentals of Spring Core & Spring Boot, explaining IoC, DI, AOP, bean scopes, transaction management, and key annotations. With this understanding, developers can effectively design and build scalable Spring-based applications.
Top comments (0)