Setting Up Spring Boot Projects

1. Creating Project with Spring Initializr

Option Values Notes
Project Maven / Gradle - Groovy/Kotlin Build system
Language Java / Kotlin / Groovy JVM language
Spring Boot 3.4.x LATEST Requires Java 17+
Packaging Jar / War Jar default; War for external Tomcat
Java 17 / 21 LTS Java 21 recommended
Dependencies Web, JPA, Security, Actuator… Searchable starter list

Example: Generate project via curl

curl https://start.spring.io/starter.zip \
  -d type=maven-project -d language=java \
  -d bootVersion=3.4.0 -d javaVersion=21 \
  -d groupId=com.example -d artifactId=demo \
  -d dependencies=web,data-jpa,actuator,security \
  -o demo.zip && unzip demo.zip

2. Using Spring Boot CLI

Command Purpose
spring init --list Show available options
spring init -d=web,jpa demo Create new project
spring run app.groovy Run Groovy script directly
spring shell Interactive shell
sdk install springboot Install via SDKMAN

3. Configuring Maven Dependencies

Element Purpose
spring-boot-starter-parent Provides dep mgmt & plugin config
spring-boot-starter-* Curated starter (web, jpa, security…)
spring-boot-maven-plugin Repackage executable jar, run goal
<scope>test</scope> Test-only dependencies

Example: Minimal pom.xml

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.4.0</version>
</parent>
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>
<build><plugins><plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
</plugin></plugins></build>

4. Configuring Gradle Dependencies

Plugin / DSL Purpose
org.springframework.boot Boot plugin (bootJar, bootRun)
io.spring.dependency-management BOM-based version management
implementation Runtime + compile classpath
testImplementation Test classpath only
annotationProcessor For Lombok / config processor

Example: build.gradle.kts

plugins {
  java
  id("org.springframework.boot") version "3.4.0"
  id("io.spring.dependency-management") version "1.1.6"
}
dependencies {
  implementation("org.springframework.boot:spring-boot-starter-web")
  implementation("org.springframework.boot:spring-boot-starter-data-jpa")
  testImplementation("org.springframework.boot:spring-boot-starter-test")
}

5. Setting Up Project Structure

Path Purpose
src/main/java Application code
src/main/resources application.yml, templates, static
src/main/resources/static Served at /
src/main/resources/templates Thymeleaf/FreeMarker templates
src/test/java JUnit tests
com.example.app
├── Application.java        (@SpringBootApplication)
├── config/                 (Java configuration)
├── web/ or controller/     (REST controllers)
├── service/                (Business logic)
├── repository/             (Spring Data interfaces)
├── domain/ or entity/      (JPA entities, DTOs)
└── exception/              (Custom exceptions, advice)

6. Configuring Application Main Class (@SpringBootApplication)

Composed Annotation Effect
@SpringBootConfiguration Marks class as config source
@EnableAutoConfiguration Triggers auto-config classpath scan
@ComponentScan Scans current package + subpackages

Example: Main class with SpringApplication

@SpringBootApplication
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}
Warning: Place main class in the root package — controllers/services in sub-packages are auto-discovered.

7. Understanding Auto-Configuration Mechanism

Concept Description
Trigger META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
Conditional Activated by @ConditionalOnClass, OnProperty, OnMissingBean
Override Define your own @Bean — auto-config backs off
Debug --debug flag prints positive/negative matches
Exclude @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

8. Customizing Application Banner

Property / File Purpose
banner.txt Place in src/main/resources
spring.banner.location Custom path
spring.main.banner-mode console / log / off
${application.version} Placeholder in banner.txt
${AnsiColor.BRIGHT_GREEN} ANSI color codes

9. Using Spring Boot DevTools

Feature Description
Auto-restart Restart on classpath change (fast, isolated classloader)
LiveReload Browser refresh on resource change (port 35729)
Property defaults Disables template caching in dev
Remote debug spring.devtools.remote.secret
Exclude spring.devtools.restart.exclude=static/**

Example: DevTools Maven dependency

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <scope>runtime</scope>
  <optional>true</optional>
</dependency>

10. Configuring Multi-Module Projects

Module Role
parent Aggregator pom with <modules>
core / domain Shared models, no Spring Boot plugin
service Business logic, library jar
web / app Executable Boot module (only one with spring-boot-maven-plugin)
Note: Only the runnable module should apply spring-boot-maven-plugin; library modules use plain jar packaging.

11. Using Parent POM Inheritance

Approach When to Use
spring-boot-starter-parent Standalone projects (default plugin config, Java version, encoding)
spring-boot-dependencies BOM Already inherit corporate parent → import as <dependencyManagement>
relativePath Empty to skip filesystem lookup

12. Managing Dependency Versions

Mechanism Description
BOM import Use spring-boot-dependencies for version alignment
Override Define <properties><jackson.version>…</jackson.version>
Gradle ext["jackson.version"] = "2.17.0"
Inspect mvn dependency:tree / gradle dependencies