AI Hints
Home›Spring Boot Tutorial›The Main Class, Running the App and Your First Endpoint
Spring Boot · Lesson 7 of 120

The Main Class

Where it all starts.

⏱ 2 min read▶ Video at 8:14🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

Every Spring Boot app has one main class. It's where the program starts, just like any Java program.

  • One main class
  • Where it starts
  • Plain Java main

BookshopApplication.java

The package line says which folder the class lives in. The imports bring in two Spring classes. At Spring Boot Application marks this as a Spring Boot app. Main is the normal Java starting point. And Spring Application dot run starts Spring, and the web server.

BookshopApplication.javajava
package com.example.bookshop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BookshopApplication {

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

One annotation, three jobs

An annotation is a label that starts with an at sign. It gives extra information to Spring. At Spring Boot Application does three jobs at once. It marks the class as configuration. It turns on auto configuration, which sets things up based on your dependencies. And it scans this package, to find your other classes.

keyvalue
Annotationa label: @Something
@Configurationthis class configures the app
Auto-configurationset up from your dependencies
Component scanfind classes in this package

Starting the app

Let's start the app, with no endpoints at all. We turn on a few extra log lines, so we can see the web server start. Run it. Spring starts Tomcat, a web server, on port eight zero eight zero. And then it says the application has started, in about two seconds.

application.propertiesini
logging.level.org.springframework.boot.tomcat=INFO
logging.level.org.apache.catalina=INFO
Output
INFO  BookshopApplication : Starting BookshopApplication using Java 17.0.18
INFO  BookshopApplication : No active profile set, falling back to 1 default profile:
"default"
INFO  TomcatWebServer : Tomcat initialized with port 8080 (http)
INFO  StandardService : Starting service [Tomcat]
INFO  StandardEngine : Starting Servlet engine: [Apache Tomcat/11.0.27]
INFO  TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
INFO  BookshopApplication : Started BookshopApplication in 1.387 seconds
Also uses _log
all

Summary

The main class has at Spring Boot Application, and calls Spring Application dot run. That starts Spring, and the Tomcat web server. Next, running the app.

Key takeaways
  • @SpringBootApplication
  • SpringApplication.run
  • Tomcat on 8080
Next up: Running the App — start, stop, restart