Let’s discover Spring

Introduction

Spring is a lightweight, enterprise-grade application framework for Java, first released in 2003. A framework is a structured foundation of reusable components and enforced architecture for rapid development.
Its inversion of control and aspect-oriented programming core enable modular, maintainable, and testable code across diverse application types.

Key features
Dependency injection and inversion of control
Aspect-oriented programming for clean separation of concerns
Auto-configuration and starter modules with Spring Boot
Extensive ecosystem: Spring MVC, Data, Security, Cloud

Common use cases
Enterprise microservices and RESTful APIs with Spring Boot
Web applications using Spring MVC and Thymeleaf
Secure authentication and authorization with Spring Security
Data access and management via Spring Data JPA
With its modular architecture, productivity-enhancing tooling, and vibrant community, Spring remains the leading choice for building robust Java applications.


What is Spring ?

Spring is an open-source Java framework that provides a comprehensive infrastructure for building robust, maintainable applications. At its core, Spring decouples object creation from application logic and offers ready-made solutions to common enterprise concerns.

  • Inversion of Control (IoC) Spring’s IoC container manages object lifecycles and relationships, moving “who creates what” out of your code and into configurable metadata or annotations.
  • Dependency Injection (DI) By annotating or declaring dependencies, you let Spring inject required collaborators automatically, reducing boilerplate and tight coupling.
  • Aspect-Oriented Programming (AOP) Cross-cutting concerns (logging, security, transactions) are modularized into aspects, keeping business logic clean.
  • Declarative Transaction Management Annotate methods or classes with @Transactional to let Spring handle commit/rollback semantics.
  • Modular Ecosystem Spring is composed of distinct projects—Core, MVC, Data, Security, Batch, Cloud, Integration—so you only pick what you need.
  • Spring Boot An opinionated layer on top of Spring that provides auto-configuration, embedded servers, and production-ready features to get applications running with minimal setup.

The Spring IoC (Inversion of Control) container takes two inputs—configuration metadata and your POJOs—and turns them into a fully configured application ready to use.

Configuration metadata tells Spring what beans to create and how to wire them (via XML files, @Component/@Bean annotations, or JavaConfig). POJOs are just your regular classes with fields and methods, unburdened by Spring-specific code.

When the container starts, it reads the metadata and:
– Instantiates each POJO as a bean
– Resolves and injects dependencies (constructor, setter, or field)
– Applies any post-processing (AOP, lifecycle callbacks)

The result is an application with all components wired together automatically—no manual new calls needed.


Example: Plain Java vs. Spring Core

A minimal scenario where a service prints a message, showing manual wiring versus Spring-managed wiring.

// A simple printer
public class Printer {
public void print(String msg) {
System.out.println(msg);
}
}


// Service that uses Printer
public class GreetingService {
private final Printer printer;

public GreetingService(Printer printer) {
this.printer = printer;
}

public void greet() {
printer.print(« Hello, World! »);
}
}


// Manual wiring in main
public class Main {
public static void main(String[] args) {
Printer printer = new Printer();
GreetingService service = new GreetingService(printer);
service.greet();
}
}


You explicitly new each object and pass dependencies by hand.

Wiring code lives in Main and must be updated whenever you change implementations.

// 1. Mark classes as Spring beans
@Component
public class Printer {
public void print(String msg) {
System.out.println(msg);
}
}


@Component
public class GreetingService {
private final Printer printer;

@Autowired
public GreetingService(Printer printer) {
this.printer = printer;
}

public void greet() {
printer.print(« Hello, World! »);
}
}


// 2. Enable component scanning
@Configuration
@ComponentScan(« com.example »)
public class AppConfig {}


// 3. Bootstrap Spring in main
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);

GreetingService service = ctx.getBean(GreetingService.class);
service.greet();
ctx.close();
}
}


@Component tells Spring to create and manage these classes as beans.

@Autowired lets Spring resolve and inject the Printer dependency automatically.

No manual new calls—Spring’s IoC container assembles the application for you.


Resources