Skip to main content

Spring Framework: Overview

Spring is a comprehensive Java framework for building enterprise applications. It provides a powerful toolkit that simplifies complex development tasks — from connecting to databases, to managing application components, to handling web requests. Spring takes care of much of the technical plumbing, allowing developers to focus on business logic.


What is Spring?

Spring is an open-source application framework that provides infrastructure support for developing Java applications. At its core, Spring manages objects (called beans) and their dependencies through a mechanism known as Inversion of Control (IoC).

Key Characteristics

CharacteristicDescription
LightweightMinimal overhead; only use the modules you need
ModularComposed of independent modules that can be used separately
Non-invasiveDoes not force classes to extend framework-specific base classes
Enterprise-readyBuilt-in support for transactions, security, messaging, and more

How Does Spring Help Developers?

Spring significantly reduces the complexity of enterprise Java development:

  • Dependency Management — Automatically wires objects together, eliminating manual instantiation and reducing coupling
  • Transaction Management — Provides declarative transaction support using @Transactional
  • Integration — Integrates seamlessly with technologies like JPA/Hibernate, Kafka, REST, messaging systems, and more
  • Testability — Encourages constructor injection and interface-driven design, making unit testing straightforward
  • Rapid Development — With Spring Boot, developers get auto-configuration, embedded servers, and production-ready defaults out of the box
  • Scalability — Spring Cloud adds tools for building distributed systems with service discovery, circuit breakers, and config servers

Spring Framework Modules

The Spring Framework is organized into well-defined modules:

ModulePurpose
Core ContainerIoC and Dependency Injection (BeanFactory, ApplicationContext)
AOPAspect-Oriented Programming for cross-cutting concerns (logging, security)
Data AccessJDBC abstraction, ORM integration, transaction management
WebSpring MVC, WebFlux (reactive), REST support
SecurityAuthentication, authorization, protection against common vulnerabilities
TestSupport for unit and integration testing with JUnit and Mockito
MessagingSupport for JMS, AMQP, Kafka integration
CloudTools for building microservices and distributed systems

Core Concepts

Inversion of Control (IoC)

IoC is a design principle where the framework controls the flow of a program rather than the developer's code. Instead of objects creating their own dependencies, the IoC container creates them and injects them where needed.

Dependency Injection (DI)

DI is the mechanism Spring uses to implement IoC. Dependencies are provided to a class from the outside rather than the class creating them itself.

Three types of injection:

TypeMechanismWhen to Use
Constructor InjectionDependencies passed via constructor parametersRecommended — ensures immutability and mandatory dependencies
Setter InjectionDependencies set through setter methods after constructionOptional dependencies that can change
Field InjectionSpring injects directly into fields via @AutowiredConvenient but harder to test
// Constructor Injection (recommended)
@Service
public class OrderService {
private final PaymentService paymentService;

public OrderService(PaymentService paymentService) {
this.paymentService = paymentService;
}
}

Spring Beans

A Spring Bean is an object created and managed by the Spring IoC container. Beans are the building blocks of a Spring application.

Bean Scopes

ScopeDescriptionUse Case
Singleton (default)One instance per application contextShared services, configuration
PrototypeNew instance every time it's requestedUser-specific data, stateful objects
RequestOne instance per HTTP requestWeb request-scoped data
SessionOne instance per HTTP sessionUser session data
Global SessionOne instance per global sessionPortlet applications

Note: Singleton beans are not thread-safe by default. Use synchronized methods, ThreadLocal, or concurrent data structures when shared state is involved.


IoC Container Types

ContainerDescriptionWhen to Use
BeanFactoryBasic container with lazy bean initializationLow-memory environments, simple applications
ApplicationContextAdvanced container with event propagation, AOP integration, internationalizationPreferred for most modern applications

Spring vs Spring Boot

AspectSpring FrameworkSpring Boot
ConfigurationManual configuration (XML or annotations)Auto-configuration with sensible defaults
ServerExternal application server requiredEmbedded server (Tomcat, Jetty, Undertow)
StartupMore setup requiredQuick start with Spring Initializr
Dependency ManagementManual dependency versionsStarter POMs manage compatible versions
Production ReadinessManual setup for health checks, metricsBuilt-in Actuator for monitoring

Key Annotations

AnnotationPurpose
@ConfigurationDeclares a class as a source of bean definitions
@BeanDefines a bean method inside a @Configuration class
@ComponentGeneric stereotype for any Spring-managed component
@ServiceSpecialization of @Component for service-layer beans
@RepositorySpecialization of @Component for data access layer
@ControllerSpecialization of @Component for web controllers
@AutowiredInjects dependencies automatically
@QualifierSpecifies which bean to inject when multiple candidates exist
@PrimaryMarks a bean as the default choice for autowiring
@ProfileAssociates a bean with a specific environment profile
@ValueInjects property values or environment variables

Design Patterns in Spring

Spring heavily utilizes well-known design patterns:

PatternUsage in Spring
SingletonDefault bean scope — one instance per container
FactoryBeanFactory and FactoryBean for creating bean instances
ProxyAOP proxies for cross-cutting concerns
Template MethodJdbcTemplate, RestTemplate, JmsTemplate
ObserverApplication event mechanism (ApplicationEvent, @EventListener)
StrategyVarious pluggable strategies (e.g., ResourceLoader, TransactionManager)

Spring Profiles

Profiles allow you to segregate application configuration by environment:

# application-dev.properties
spring.datasource.url=jdbc:h2:mem:devdb

# application-prod.properties
spring.datasource.url=jdbc:postgresql://prod-host:5432/proddb

Activating a profile:

  • Command line: -Dspring.profiles.active=prod
  • Properties file: spring.profiles.active=prod
  • Programmatically: SpringApplication.setAdditionalProfiles("prod")

When to Use Spring

  • DO use Spring for enterprise Java applications that need robust DI, transaction management, and integration support
  • DO use Spring Boot for rapid application development with minimal configuration
  • DO use Spring Cloud for building microservice architectures
  • DON'T use Spring when a simple, lightweight solution suffices — avoid over-engineering
  • DON'T fight the framework — embrace convention over configuration

Advanced Editorial Pass: Framework Foundations and Architectural Cost

Strategic Perspective

  • Spring is an inversion-of-control platform, not only a library bundle.
  • Design quality depends on how clearly you define bean ownership and boundaries.
  • Framework flexibility can amplify both good architecture and bad coupling.

High-Risk Patterns

  • Treating DI as global mutable state with weak module boundaries.
  • Overusing annotations without explicit lifecycle and contract reasoning.
  • Pushing domain decisions into framework glue layers.

Senior Review Questions

  1. Can each module be reasoned about without loading the full application context?
  2. Are lifecycle assumptions explicit and testable?
  3. Which framework feature introduces the largest long-term lock-in risk?

Compare Next