Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

What Is Spring? Beginner Guide, Uses & Examples

What is Spring?

Spring is the most popular open-source Java application development framework. Initially written by Rod Johnson and first released in June 2003, Spring provides comprehensive infrastructure support for developing Java applications. It lets you focus on your application's business logic while Spring handles the infrastructure.

Spring's core philosophy is Inversion of Control (IoC) - instead of your code creating and managing objects, the Spring tl-container creates and manages them for you. This is achieved through Dependency Injection (DI), where dependencies are "injected" into objects rather than objects creating their own dependencies.

Spring Ecosystem

ModuleDescription
Spring CoreIoC container, DI, Bean lifecycle management
Spring MVCWeb MVC framework with DispatcherServlet
Spring BootAuto-configuration, embedded server, production-ready features
Spring DataSimplified data access (JPA, MongoDB, Redis, etc.)
Spring SecurityAuthentication, authorization, OAuth2, JWT
Spring CloudMicroservices, service discovery, config server
Spring BatchBatch processing for large datasets
Spring IntegrationEnterprise integration patterns
Spring AOPAspect-Oriented Programming support
Spring WebFluxReactive, non-blocking web framework

Spring vs EJB

FeatureSpringEJB (Enterprise JavaBeans)
ContainerLightweight IoC containerHeavy EJB tl-container (full Java EE server)
DeploymentAny servlet tl-container (Tomcat)Requires full Java EE server (JBoss, GlassFish)
TestingEasy unit testing (POJO-based)Difficult (requires container)
ConfigurationAnnotations or XMLComplex XML descriptors
Learning CurveModerateSteep
PerformanceExcellentGood (but heavier)
PopularityVery highDeclining
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
// 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 Application 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

Ready to Level Up Your Skills?

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