The Spring Framework is an open-source Java framework used to build loosely coupled applications. It provides infrastructure support for Java applications and simplifies development through features such as dependency injection, modular components, web development, data access, and transaction management.
- Spring promotes loose coupling between application components.
- It provides a modular architecture, allowing developers to use only the features required by an application.
How Spring Framework Works
Spring Framework uses an IoC container to create, manage, and connect application objects. It automatically provides required dependencies through Dependency Injection (DI).

The basic working process is:
- Spring reads configuration metadata provided through Java configuration, annotations, or XML.
- The IoC container uses this configuration to identify application components.
- It creates and manages objects called Spring Beans.
- The container resolves relationships between the beans.
- Required dependencies are injected into the appropriate beans.
- The configured beans become available for use by the application.
For example, consider a service that depends on a repository:
public class UserService{
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
Instead of UserService creating a UserRepository itself, the required dependency can be provided from outside. This approach reduces tight coupling and makes the application easier to maintain and test.
Key Features of Spring Framework
- Dependency Injection: Automatically provides required dependencies to classes, promoting loose coupling and easier application maintenance.
- Aspect-Oriented Programming (AOP): Separates common concerns like logging, security, and transactions from the main business logic.
- Transaction Management: Provides consistent support for managing transactions across different data-access technologies.
- Spring MVC: Provides a structured MVC architecture for developing scalable and maintainable Java web applications.
- Spring Security: Provides comprehensive support for authentication, authorization, and protection against common security vulnerabilities.
- Spring Data: Simplifies database access and data management across relational and non-relational databases.
- Spring Batch: Provides reliable support for processing large volumes of data through scheduled batch operations.
- Integration: Provides easy integration with technologies such as Hibernate, JPA, JMS, and other enterprise frameworks.
Architecture of Spring Framework
The Spring Framework follows a modular architecture. Its functionality is divided into different areas so developers can use the components required by their applications.

The major areas include:
- Core Container: The core component providing the IoC container for managing beans and their dependencies. It includes BeanFactory and ApplicationContext for object creation and dependency injection.
- Spring AOP Module: Implements Aspect-Oriented Programming to handle cross-cutting concerns like transaction management, logging and monitoring, using aspects defined with the @Aspect annotation.
- Spring ORM Module: Provides integration with ORM technologies such as Hibernate, JPA, and MyBatis, simplifying transaction management and exception handling for data access.
- Spring Web MVC Module: Implements the MVC architecture to create web applications. It separates model and view components, routing requests through the DispatcherServlet to controllers and views.
- Spring DAO Module: Provides data access support through JDBC, Hibernate or JDO, offering an abstraction layer to simplify database interaction and transaction management.
- Spring Application Context Module: Builds on the Core module, offering enhanced features like internationalization, validation, event propagation and resource loading via the ApplicationContext interface.
Core Concepts of Spring Framework
Several concepts form the foundation of Spring development.
1. Inversion of Control (IOC) Container
Inversion of Control (IoC) is a design principle in which control over object creation and dependency management is transferred to a framework or container.
Without IoC:
UserRepository repository = new UserRepository();
UserService service = new UserService(repository);
The application explicitly creates and connects its objects. With Spring, the container can manage these objects and their relationships.
A simplified flow is:
Configuration -> IoC Container -> Bean Creation -> Dependency Injection -> Application
2. Dependency Injection
Dependency Injection is a technique through which an object's dependencies are supplied from outside rather than being created by the object itself.
- Spring commonly supports dependencies through constructors and setters.
- Constructor injection is generally preferred when a dependency is required.
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
Here, Spring can provide the required UserRepository implementation when it creates UserService.
3. Spring Beans
A Spring Bean is an object whose lifecycle is managed by the Spring IoC container. Beans can be registered using annotations such as:
@Component
public class EmailService {
}
They can also be explicitly declared using Java configuration:
@Configuration
public class AppConfig {
@Bean
public EmailService emailService() {
return new EmailService();
}
}
In both cases, Spring manages the resulting object as a bean.
4. Spring Annotation
Spring Annotations provide metadata that Spring uses to configure and manage application components.
Some commonly used annotations are:
- @Configuration: Defines a Java class that contains Spring configuration and bean definitions.
- @Bean: Defines and registers an object as a bean managed by the Spring IoC container.
- @Component: Marks a Java class as a general component managed automatically by the Spring container.
- @Service: Marks a class as a service-layer component that typically contains application business logic.
- @Repository: Marks a class as a data-access component responsible for interacting with the persistence layer.
- @Controller: Marks a class as a Spring MVC controller responsible for handling incoming web requests.
- @Autowired: Enables automatic dependency injection by allowing Spring to provide the required bean dependency.
- @ComponentScan: Specifies the packages that Spring should scan to discover and register application components.
Simple Spring Framework Example
Consider an application where NotificationService requires a MessageService.
First, create the MessageService interface:
public interface MessageService {
void sendMessage();
}
Implement the interface:
@Component
public class EmailService implements MessageService {
@Override
public void sendMessage() {
System.out.println("Email sent successfully.");
}
}
Now create NotificationService:
@Component
public class NotificationService {
private final MessageService messageService;
public NotificationService(MessageService messageService) {
this.messageService = messageService;
}
public void notifyUser() {
messageService.sendMessage();
}
}
Create a configuration class:
@Configuration
@ComponentScan("com.example")
public class AppConfig {
}
Finally, start the Spring container and access the bean:
public class Main{
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
NotificationService service =
context.getBean(NotificationService.class);
service.notifyUser();
}
}
Output:
Email sent successfully.
In above example, Spring discovers EmailService and NotificationService, creates their objects, and injects the MessageService implementation into NotificationService.
Download Complete Example
Download the complete runnable Spring Framework example as a ZIP file to explore and run the code locally.