Dev.to WebDev šŸ›  Dev šŸ‘ 2 šŸ“– 6 min read

Your Spring Boot App Starts in 3 Seconds. Here's How

You click Run. The console fills with lines you don't read. A few seconds later, one line appears that you do read: Started DemoApplication in 2.9 seconds. Your API is live. So what happened in those 3 seconds? A Spring

Your Spring Boot App Starts in 3 Seconds. Here's How

You click Run. The console fills with lines you don't read. A few seconds later, one line appears that you do read: Started DemoApplication in 2.9 seconds. Your API is live.

So what happened in those 3 seconds? A Spring Boot application is a normal Java program whose main() hands control to Spring. Spring then finds your classes, turns them into objects called beans, plugs them together, sets up a web server for you, and starts listening on port 8080.

That's the whole story. Now let's slow it down, because once you can see these steps, the scary startup errors stop being scary.

A tiny app to follow

Here is a small bookshop app. Three classes, all in the package com.example.demo (or packages below it):

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

@Service
class BookService {
    List<String> all() { return List.of("Dune", "Emma"); }
}

@RestController
class BookController {
    private final BookService books;

    BookController(BookService books) {   // Spring passes this in
        this.books = books;
    }

    @GetMapping("/books")
    List<String> list() { return books.all(); }
}

Notice what's missing. There is no new BookService() anywhere. There is no server setup. You never tell anyone about port 8080. Hold on to that, we'll see who does all of it.

Watch the 3 seconds

Spring Boot startup: main(), component scan, beans, auto-configuration, Tomcat, started

Step 1: main() hands over the keys

Java doesn't know anything about Spring. It does what it always does: it finds main() and runs it.

The only line inside is SpringApplication.run(...). From that moment, Spring is in charge. It reads your settings (like application.properties) and creates the application context. Think of the context as a big box that will hold every object your app needs.

Step 2: Component scan makes a guest list

Look at @SpringBootApplication again. It's really three annotations in one coat:

  • @ComponentScan: go and look for my classes
  • @EnableAutoConfiguration: set things up based on my dependencies
  • @SpringBootConfiguration: this class holds configuration

Component scan starts in the package of your main class, com.example.demo, and walks through every package below it. Any class marked @Component, @Service, @Repository, @Controller or @RestController goes on the guest list.

That last part matters a lot. A class in com.example.demo.web is found. A class in com.example.mail is not, even if it has @Service on it. It's a sibling, not a child. Keep that in mind for the error section.

Step 3: Beans are built and plugged together

Every class on the guest list becomes a bean.

So what is a bean in Spring Boot? Just an object that Spring creates and looks after for you, instead of you calling new. By default Spring makes one of each and shares it everywhere.

Now the clever part. Spring looks at each constructor:

BookController(BookService books)

"To build a BookController, I need a BookService first." So Spring builds the BookService, then passes it into the controller's constructor. Dependencies first, then the things that need them. Like building a car: engine before the car.

This is called dependency injection, and using the constructor for it is the recommended way. If your class has only one constructor, you don't even need @Autowired. Spring uses it automatically.

Why bother? Because BookController never decides which BookService it gets. In a test, you can hand it a fake one. Your classes stay small and easy to swap.

šŸ’” This is how the Spring Boot course on DevTrail starts too: you write beans and wire them together, get your code checked right in your browser, and practise reading real startup errors like the ones below.

Step 4: Auto-configuration reads your shopping list

Your pom.xml has one important line: spring-boot-starter-web. A starter is a bundle of libraries that usually go together.

At startup, Spring Boot checks which libraries are on your classpath and sets things up to match:

You added... Spring Boot gives you
spring-boot-starter-web Embedded Tomcat, Spring MVC (routing), Jackson (Java ⇄ JSON)
spring-boot-starter-data-jpa + a database driver A database connection pool and JPA
spring-boot-starter-validation A validator for @Valid checks

That's auto-configuration. It's not magic, it's "if this library is here, and you didn't set it up yourself, here's a sensible default". If you define your own bean of the same kind, Spring Boot steps aside and uses yours.

Step 5: Tomcat opens the door

In the old days, you installed a server like Tomcat and dropped your app into it. Spring Boot flips that around: Tomcat lives inside your app. Auto-configuration starts it, and it opens port 8080.

Then you see the line you were waiting for:

Tomcat started on port 8080 (http)
Started DemoApplication in 2.9 seconds

Open http://localhost:8080/books and you get ["Dune","Emma"]. Your startup time will be different on your machine, of course. Small apps usually take a few seconds.

How to run a Spring Boot application: 3 ways

All three do the same startup you just watched. They differ in when you'd use them.

Way Command Best for
IDE run button Click ā–¶ next to main() Everyday coding and debugging
Maven plugin ./mvnw spring-boot:run Running from a terminal without packaging
Executable jar ./mvnw package then java -jar target/*.jar Servers, Docker, anything you deploy

A few small notes:

  • ./mvnw is the Maven wrapper that comes with every new project, so you don't need Maven installed. On Windows, use mvnw instead.
  • On Gradle projects, the second one is ./gradlew bootRun.
  • The jar holds everything, Tomcat included. The server only needs Java 17 or newer (Spring Boot 3 needs at least Java 17).
  • Stop a running app with Ctrl + C.

The 2 startup errors everyone hits

Both appear in a big box that says APPLICATION FAILED TO START. Don't panic. Spring is very good at telling you exactly what's wrong. Read the Description line.

Error 1: "Port 8080 was already in use"

Description:
Web server failed to start. Port 8080 was already in use.

Only one program can listen on a port at a time. Very often, the "other program" is your own app from an earlier run, still alive in another terminal or IDE tab.

Two fixes:

  1. Stop the old one. Find it and end it:
    • macOS/Linux: lsof -i :8080, then kill <PID>
    • Windows: netstat -ano | findstr :8080, then taskkill /PID <PID> /F
  2. Use another port. Add server.port=8081 to src/main/resources/application.properties, or for one run: java -jar target/*.jar --server.port=8081.

Error 2: "required a bean ... that could not be found"

Say you add a new EmailService and ask for it in the constructor of a SignupController. You press Run and get:

Description:
Parameter 0 of constructor in com.example.demo.SignupController required
a bean of type 'com.example.mail.EmailService' that could not be found.

You may also see the cousin of this message: No qualifying bean of type .... Same meaning: "you asked for this object in a constructor, but it's not in my box."

Think back to Step 2 and Step 3. There are two usual reasons:

  1. The class is outside the main package. Here, EmailService lives in com.example.mail, a sibling of com.example.demo. Component scan never walked in there. Fix: move it under com.example.demo (for example com.example.demo.mail).
  2. You forgot the annotation. A class without @Service or @Component is just a class. Spring won't create it. Fix: add the annotation.

Once you know the startup story, this error reads like a sentence: "I scanned, I didn't find it, so I couldn't build the controller."

What you'd learn next

You now know what happens between clicking Run and seeing "Started". A real backend needs a lot more:

  • āœ… How a Spring Boot app starts, and what a bean is (this post)
  • šŸ”’ Spring Core Fundamentals: bean scopes, @Configuration, properties and profiles
  • šŸ”’ Building REST APIs: path variables, request bodies, status codes
  • šŸ”’ Layered Architecture: controllers, services and repositories
  • šŸ”’ Database and Spring Data JPA
  • šŸ”’ Validation and Exception Handling
  • šŸ”’ Spring Security
  • šŸ”’ Testing Spring Boot Applications
  • šŸ”’ API Documentation and Developer Tools
  • šŸ”’ Docker and Deployment
  • šŸ”’ Microservices Fundamentals
  • šŸ”’ Real-World Spring Boot Projects you can put on your CV

šŸš€ Go from zero to hero with Spring Boot

This post covered step 1 of 13. The full Learn Spring Boot from Basic to Advanced course on DevTrail takes you through all of it: 13 modules and 86 hands-on lessons, ending with real projects.

Every lesson has exercises and quizzes you check right in your browser, so you learn by doing, not by watching.

Module 1 is free. Start the Spring Boot course →

šŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.