Tutorials Logic, IN info@tutorialslogic.com

Spring Framework and Spring Boot Introduction

Spring Application Model

Spring is a Java application framework centered on an inversion-of-control container. The container creates application objects, connects their dependencies, applies lifecycle and infrastructure behavior, and lets business code depend on explicit contracts.

Spring Framework supplies the core container and libraries. Spring Boot builds on it with dependency starters, auto-configuration, executable applications, externalized configuration, and production support. Spring Boot 4.1 requires Java 17 or later.

Spring Core

Inversion of Control means object creation and wiring move from application classes to a container. Dependency Injection is the practical mechanism: a class declares what it needs through its constructor, and the container supplies matching beans.

Constructor injection keeps required dependencies visible, supports immutable fields, and lets an ordinary unit test instantiate the class without starting Spring. Component scanning is convenient, but explicit configuration remains useful when construction needs a third-party class or a deliberate choice.

Spring Ecosystem

Module Description
Spring Core IoC container, DI, Bean lifecycle management
Spring MVC Web MVC framework with DispatcherServlet
Spring Boot Auto-configuration, embedded server, production-ready features
Spring Data Simplified data access (JPA, MongoDB, Redis, etc.)
Spring Security Authentication, authorization, OAuth2, JWT
Spring Cloud Microservices, service discovery, config server
Spring Batch Batch processing for large datasets
Spring Integration Enterprise integration patterns
Spring AOP Aspect-Oriented Programming support
Spring WebFlux Reactive, non-blocking web framework

Framework and Boot

Feature Spring Framework Spring Boot
Foundation Core container and application libraries Opinionated setup built on Spring Framework
Configuration Choose and configure framework modules Auto-configuration backs off when explicit beans are supplied
Dependencies Select individual modules Starters provide tested dependency sets
Runtime Can be packaged for a chosen environment Commonly runs as an executable application with an embedded server
Operations Integrate monitoring as needed Actuator adds health, metrics, and management endpoints
Use together Defines the programming model Provides the normal application bootstrap and conventions
Escape hatch Declare beans and infrastructure explicitly Exclude or replace an unwanted auto-configuration

IoC and DI Concept

IoC and DI Concept
// WITHOUT Spring: tight coupling, hard to test
public class OrderService {
    // Creates its own dependency - tightly coupled!
    private EmailService emailService = new EmailService();
    private PaymentService paymentService = new PaymentService();

    public void placeOrder(Order order) {
        paymentService.processPayment(order);
        emailService.sendConfirmation(order);
    }
}

// Problem: Can't easily swap EmailService with MockEmailService for testing
// Problem: OrderService is responsible for creating its dependencies

Spring Boot Application Entry Point

Spring Boot Application Entry Point
// WITH Spring: loose coupling, easy to test
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class OrderService {
    // Spring injects these - loosely coupled!
    private final EmailService emailService;
    private final PaymentService paymentService;

    // Constructor injection (recommended)
    @Autowired
    public OrderService(EmailService emailService, PaymentService paymentService) {
        this.emailService   = emailService;
        this.paymentService = paymentService;
    }

    public void placeOrder(Order order) {
        paymentService.processPayment(order);
        emailService.sendConfirmation(order);
    }
}

// Spring creates and injects EmailService and PaymentService automatically
// Easy to test: inject mock implementations in unit tests

Spring Boot Entry Point

Spring Boot Entry Point
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        // Starts the embedded Tomcat server and Spring context
        SpringApplication.run(MyApplication.class, args);
    }
}

// Run: mvn spring-boot:run
// Or: java -jar target/myapp.jar
// Access: http://localhost:8080

Application Layers

A typical web application separates HTTP concerns, business rules, and persistence. A controller validates transport input and chooses a response, a service coordinates a use case and transaction, and a repository handles storage. The names are less important than keeping dependencies pointed toward stable application rules.

Layer Owns Avoid
Controller HTTP mapping, status, request DTO validation Business transactions and SQL
Service Use-case rules and transaction boundary Servlet APIs and view formatting
Repository Queries and persistence mapping Authorization and HTTP responses
Configuration Bean creation and infrastructure choices Feature behavior

Configuration and Profiles

Spring Boot reads external configuration from ordered property sources. Bind related settings to a validated @ConfigurationProperties type instead of scattering @Value strings. Profiles select groups of beans or configuration, but they should not become a substitute for versioned environment settings.

Never commit production secrets. Supply them through the deployment platform or a secret manager, and fail startup when required configuration is absent or invalid.

Testing Scope

Use a plain unit test for one class and mocked collaborators. Use a focused test slice when framework behavior such as MVC binding or data mapping is the subject. Use @SpringBootTest only when the complete application context and integrated wiring are genuinely required.

  • Keep constructor-injected services testable without the container.
  • Test validation, authorization, transaction rollback, and error responses at their owning boundary.
  • Start real infrastructure through an isolated test environment when an in-memory substitute changes behavior.
  • Expose only the Actuator endpoints needed by operations and secure them separately from public traffic.
Before you move on

First Spring Application Review

5 checks
  • Explain IoC and constructor injection without relying on annotations alone.
  • Distinguish Spring Framework from Spring Boot.
  • Trace a request through controller, service, and repository boundaries.
  • Bind and validate external configuration without committing secrets.
  • Choose unit, slice, or full-context testing deliberately.

Spring Design Failures

  • Field injection hides requirements

    Declare required collaborators in the constructor.
  • Full context test for every class

    Use the smallest test scope that proves the behavior.
  • Controller owns a transaction

    Move the business use case and transaction boundary into a service.
  • Auto-configuration treated as magic

    Inspect the condition report and declare or exclude the competing bean deliberately.
Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.