back

Spring Boot - Introduction

14 min read4 views0 reactions
SpringbootSpringJavaSpring BootSpring framework

Spring Boot has quickly become the go-to framework for building production-ready applications with minimal setup. It abstracts away a lot of boilerplate, offers built-in starters, and makes deployment effortless. But WHY SPRINGBOOT?

Why Spring Boot?

There are 3 major problems that spring boot solves:

NOTE: Don't worry about this jargon, we'll cover each term later.

What is Spring?

Spring is a framework that helps you create enterprise Java application. It enables you to build Java apps from POJO (Plain Old Java Obj). Provides a comprehensive ecosystem (Spring MVC, Spring Data, Spring Security, etc.) for building scalable applications.

Spring Boot builds on top of Spring Framework, it still uses DI/IoC under the hood, but adds auto-configuration and starter dependencies so you don’t have to write as much configuration code manually.

Spring = IoC + DI
Spring Boot = Spring + Auto Configuration + Starters + Embedded Server

Dependency Injection & IOC

Java application typically consists of objects that collaborate to form one proper application.
Many objects in the application have dependencies on each other.

Although Java platform provides many application development functionality, it lacks the means to organize all these building blocks.

IOC component addresses this concern by providing a formalized means of composing disparate components into a fully working application.

Modules:

Spring framework consists of 20 modules, grouped into:

1) Core Container:

Consists of Core, Beans, Context & Expression Language.

2) Data Access/Integration:

Consists of JDBC, ORM, OXM, JMS and Transaction modules.

3) AOP (Aspect-Oriented Programming)

4) Instrumentation

5) Web:

Consists of web, web-servlet, web-struts, web-Portlet.

6) Test:

IOC & DI

In simple words:

DI:

Real Life Analogy:
Imagine yourself at a restaurant:

Spring History

First, let's cover some Spring basics before we get into Spring Boot.

Spring Framework

1) Inversion of Control (IOC):

Example:

2) Dependency Injection (DI):

Example:

@Component  
public class OrderService {  
    private final PaymentService paymentService;  

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

3) Spring Beans and Bean Lifecycle

Example:

@Component
public class MyBean {

    @PostConstruct
    public void init() {
        System.out.println("Bean initialized");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("Bean about to be destroyed");
    }
}

4) Configuration & @Configuration

Example:

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyService();
    }
}

5). Component Scanning

6). Spring Annotation Overview

7). Spring MVC

Key Annotations:

Example:

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello";
    }
}

8). Spring ApplicationContext

Key Points:

9). Aspect-Oriented Programming (AOP)

Example:

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.service.*(..))")
    public void logBefore(JoinPoint jp) {
        System.out.println("Method called: " + jp.getSignature());
    }
}

10). Spring Data JPA

Key Annotations:

Example:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    User findByUsername(String username);
}

Spring Boot:

Spring Boot is a Java framework that makes it easy to create stand-alone, production-ready Spring applications with very little setup.

An extended version of Spring framework (annotations).

It removes the need for complex configuration by providing:

Spring Framework + AutoConfig + Conventions + Tools = Spring Boot

Internal Working of Spring Boot

Internal working of Spring boot - visual representation:

Internal working of springboot

What happens when you run a Spring Boot Application

  1. JVM starts the app and main() method is executed

  2. SpringApplication.run(MyApp.class, args) → Inside this run method, inside this again run method is executed

  3. SpringBoot detects @SpringBootApplication on MyApp.class which includes:

    • @SpringBootConfiguration → marks as @Configuration

    • @ComponentScan → scans current package & sub-packages

    • @EnableAutoConfig → loads auto-config from Spring factories

  4. Creates a SpringApplication object:

    • Sets up Application Context

    • Sets up Environment

    • Registers Listeners, Initializers

  5. Application Context is created

  6. Environment is prepared (e.g. application.properties, args)

  7. Beans are scanned and registered via @ComponentScan and Auto-configured beans are registered (e.g. DataSource, WebServer)

  8. ApplicationContext is refreshed

    • All beans are created

    • Dependencies are autowired

    • Lifecycle methods are called

  9. Embedded web server (e.g., Tomcat) is started. Binds it to a port (8080), registers DispatcherServlet as front controller of MCV

  10. CommandLineRunner or ApplicationRunner beans (if any) are executed

  11. Application is now fully started and ready to serve requests. And handles all HTTP requests

Internal Working - Detailed version

1. Application Entry Point (Main Class)

Every Spring Boot application starts with a main() method annotated with @SpringBootApplication.

@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

This calls:

SpringApplication.run(MyApp.class, args);

This is the starting point of the Spring Boot engine.


2. @SpringBootApplication Explained

This is a combination of 3 annotations:

@Configuration         // Marks this class as a configuration source
@EnableAutoConfiguration  // Triggers auto-configuration
@ComponentScan        // Enables component scanning for current package & subpackages

So it:


3. SpringApplication.run()

This is the heart of Spring Boot. It performs these steps internally:

a. Creates a SpringApplication object

This prepares the context, listeners, and initializers.

b. Loads ApplicationContext

c. Applies Auto-Configuration

Example:
If Spring finds spring-boot-starter-web, it auto-configures:


4. Embedded Server Setup

If it’s a web application:


5. Component Scanning and Bean Creation

Spring scans the package of the main class for:

Each is created as a Spring Bean and stored in the ApplicationContext.

Dependency Injection is applied wherever @Autowired is used.


6. Handling HTTP Requests

When a request like GET /hello hits the server:

  1. The embedded server receives the request

  2. The DispatcherServlet (front controller) handles it

  3. It uses HandlerMapping to find the matching @Controller method

  4. The method is executed, and response is returned (e.g., JSON)

Example:

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hi!";
    }
}

7. External Configuration

Spring Boot reads external configuration from:

These values are injected using @Value or bound via @ConfigurationProperties.


8. Actuator, Metrics & Monitoring (Optional)

If you add spring-boot-starter-actuator:


Summary Diagram (Text-based)

          main() ➜ SpringApplication.run()
                        |
                        ↓
              Creates ApplicationContext
                        |
        Auto-configuration & Component Scan
                        |
          Instantiates Embedded Server (Tomcat)
                        |
           Registers DispatcherServlet
                        |
            Scans and wires Beans (DI)
                        |
            Handles incoming HTTP requests

Bonus: Conditional Auto-Config Example

@Bean
@ConditionalOnMissingBean
public DataSource dataSource() {
    // only used if you don't define your own DataSource
}

This is why Spring Boot is smart — it only configures defaults when you don’t.

What is Dispatcher Servlet:

Working:

  1. Receives the request

  2. Finds matching controller using Handler Mapping

  3. Calls the controller method

  4. Processes the return value (like view/json)

  5. Sends the final response

Internal Working:

Request Flow Example:

Let’s say you hit /hello GET request

  1. DispatcherServlet receives /hello GET request

  2. It checks for a controller method mapped to /hello

  3. Let’s say it finds this:

    @RestController  
    public class HelloController {

        @GetMapping("/hello")  
        public String helloWorld() {  
            return "hello world";  
        }

    }
  1. It invokes helloWorld() method

  2. Sends "hello world" as a response to browser

Next in the series, we’ll explore how to create a REST API in Spring Boot, step by step. Till then Happy Learning.