Coding for All
Lesson 1: The Smallest Server
The Smallest Server Start a real web server with three lines of your own code.
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3
4@SpringBootApplication
5public class DemoApplication {
6 public static void main(String[] args) {
7 SpringApplication.run(DemoApplication.class, args);
8 }
9}
Terminal
$ ./mvnw spring-boot:run
:: Spring Boot :: (v3.3.0)
Tomcat started on port 8080 (http)
Started DemoApplication in 1.24 seconds
(the server is now running, waiting for requests...)
Every line, explained
import org.springframework.boot.SpringApplication;
Two locked imports bring in the heart of Spring Boot: the SpringApplication class that starts everything...
import org.springframework.boot.autoconfigure.SpringBootApplication;
...and the @SpringBootApplication annotation you are about to type.
@SpringBootApplication
Your first annotation! An annotation is a label starting with @ that you attach to a class or method to give the framework information. This one means "this class is the starting point of a Spring Boot app: set everything up from here". One label, and Spring wires up a whole web server.
public class DemoApplication {
A completely normal Java class, like the ones from Your First Class. Frameworks don't replace the language; they build on it.
public static void main(String[] args) {
The same main method every Java program starts with. Nothing framework-y here yet.
SpringApplication.run(DemoApplication.class, args);
This is the moment you hand control to the framework. SpringApplication.run(...) starts the whole machine: it reads your annotations, starts a web server called Tomcat on port 8080, and then waits for requests. This is the big idea of a framework: instead of your code calling a library, the framework runs the show and calls YOUR code at the right moments.
}
This } closes the method.
}
This } closes the class.