Skip to main content

Command Palette

Search for a command to run...

#7 Understanding Spring Boot Project Structure & Application Startup Flow

Published
3 min readView as Markdown

Using STS IDE – Spring Boot Directory Structure

When you create a Spring Boot project using
Spring Tool Suite
(or Spring Initializr), the structure looks like this:

Standard Spring Boot Directory Structure

project-name
│
├── src/main/java
│     └── com/example/demo
│            └── DemoApplication.java
│
├── src/main/resources
│     ├── application.properties
│     ├── static
│     ├── templates
│
├── src/test/java
│
└── pom.xml

src/main/java

  • Contains your Java source code

  • Controllers, Services, Repositories

  • Main class (Entry point)

src/main/resources

Contains:

pom.xml

  • Maven configuration

  • Dependencies

  • Build settings

@SpringBootApplication (Entry Point)

Inside DemoApplication.java:

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

What is @SpringBootApplication?

It is a combination of 3 annotations:

@SpringBootApplication =
    @Configuration
    + @EnableAutoConfiguration
    + @ComponentScan
  1. @EnableAutoConfiguration

Automatically configures:

  • Tomcat

  • DispatcherServlet

  • DataSource

  • Jackson (JSON)

  • Hibernate

Based on:

  • Dependencies present in pom.xml

Example:
If you add:

spring-boot-starter-web

Spring automatically:

  • Starts embedded Tomcat

  • Configures MVC

  • Enables REST

No manual XML configuration needed.

  1. @ComponentScan

Spring scans:

  • Current package

  • Sub-packages

For:

  • @Controller

  • @Service

  • @Repository

  • @Component

Then creates Beans in Spring container.

Important Rule:
Main class should be placed at root package.


  1. @Configuration

Indicates:

  • This class defines beans

  • Acts like XML configuration

How Many Ways Can We Provide Properties to Spring Boot?

There are 3 main ways:

  1. application.properties / application.yml

Located in:

src/main/resources

Example:

server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/test

Or YAML:

server:
  port: 8081
  1. Environment Variables (After Deployment)

Used when:

  • Application is deployed

  • You don’t want to rebuild JAR again and again

Example (Linux):

export SERVER_PORT=9090

Spring Boot automatically reads environment variables.

Very useful in:

  • Production

  • Docker

  • Cloud


  1. Command Line Arguments (Startup Time)

While starting application:

java -jar app.jar --server.port=9095

This overrides:

Priority Order (Very Important)

Command line arguments (highest priority)
↓
Environment variables
↓
application.properties (lowest)

SpringApplication.run() – What Does It Take Care Of?

SpringApplication.run(DemoApplication.class, args);

This is the heart of Spring Boot startup.

It performs:

  1. Creates Spring Container (ApplicationContext)
  • Initializes IOC container

  • Creates all beans

  • Manages dependency injection

  1. Performs Component Scanning
  • Scans packages

  • Registers @Controller, @Service, etc.

  1. Enables Auto Configuration
  • Configures embedded Tomcat

  • Configures MVC

  • Configures DataSource (if present)

  • Configures JSON

  1. Starts Embedded Server

If web dependency present:

  • Starts Tomcat automatically
  1. Applies External Configuration
  • Reads properties

  • Applies environment variables

  • Applies command-line arguments

What SpringApplication.run() Actually Returns?

It returns:

ApplicationContext object

Which contains:

  • All beans

  • Environment

  • Configurations

Complete Startup Flow

main()
   ↓
SpringApplication.run()
   ↓
Create ApplicationContext
   ↓
Auto Configuration
   ↓
Component Scan
   ↓
Bean Creation
   ↓
Start Embedded Tomcat
   ↓
Application Ready