Stop 14 · Spring Boot & Backend DevelopmentTopics · 3/13

Spring Boot Basics & Starters

Part of the Java Roadmap

In short

Plain Spring used to need lots of XML and a separate Tomcat install. Spring Boot gives you a running web server with one class and one dependency, so you start building features on day one.

Spring Boot sets up a Spring application with sensible defaults, an embedded server and starter dependencies like spring-boot-starter-web, so you can run a backend with very little configuration.

The jaadu walks into a fully furnished flat carrying only a small suitcase, looking delighted at the ready sofa, bed, fridge and curtains.

Think of it like this

The fully furnished flat

Renting an empty flat in Gurugram means buying a fridge, bed, curtains and gas connection yourself. A fully furnished flat comes ready, and you only replace what you don't like. Spring Boot starters are the furniture packs, auto-configuration sets everything up, and application.properties is where you swap the curtains.

Watch it happen

What happens when you run the app

main()
Component scan
Auto-config
Tomcat
Ready

1/5SpringApplication.run starts the application context.

The big ideas

What you need to know

Starters

spring-boot-starter-web, -data-jpa, -validation, -security and -test bring matching, tested library versions.

Auto-configuration

Boot configures beans based on what's on the classpath and your properties. Your own beans win over the defaults.

application.properties

Port, database URL, logging levels and more. Profiles like application-dev.properties override per environment.

Executable jar

mvn package builds one jar with the server inside. Run it anywhere with java -jar app.jar.

See it in code

Walk through the code

java
1// src/main/java/com/example/shop/ShopApplication.java2package com.example.shop;3 4import org.springframework.boot.SpringApplication;5import org.springframework.boot.autoconfigure.SpringBootApplication;6import org.springframework.web.bind.annotation.GetMapping;7import org.springframework.web.bind.annotation.RestController;8 9@SpringBootApplication10public class ShopApplication {11    public static void main(String[] args) {12        SpringApplication.run(ShopApplication.class, args);13    }14}15 16@RestController17class HealthController {18    @GetMapping("/hello")19    String hello() {20        return "Namaste from Spring Boot";21    }22}23 24// src/main/resources/application.properties25// server.port=808126// spring.application.name=shop

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

Generate the project at start.spring.io with the Web starter, add the controller and run ./mvnw spring-boot:run.

Save yourself the pain

Common mistakes

Oops

Putting the main class in a deep sub-package and controllers elsewhere.

Do this instead

Keep the main class at the root package, like com.example.shop, so everything below is scanned.

Oops

Adding version numbers to every Spring dependency by hand.

Do this instead

The Spring Boot parent or BOM manages versions. Leave them out for starter dependencies.

Oops

Hard-coding passwords in application.properties in Git.

Do this instead

Use environment variables, which Spring maps automatically, like SPRING_DATASOURCE_PASSWORD.

Your mission

Hello, Spring Boot

~20 min

Quick check

Which three annotations does @SpringBootApplication combine?

Go deeper

  1. 1Generate a project at start.spring.io with the Web starter and run it.
  2. 2Explore application.properties and change the server port.
  3. 3Read the startup log to see auto-configuration at work.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge