Spring Boot is a framework that creates ready-to-run web applications without dozens of lines of XML with a “convention over configuration” philosophy. The server appears in one line, java -jar, including the built-in Tomcat.
Learning Objectives
- Know the project structure provided by Spring Initializr
- Know the role of
@SpringBootApplication - Understand the concept of dependency injection (DI)
- Change basic settings such as port and log with
application.properties - Run as
mvn spring-boot:run
Key concepts
1) Spring Initializr
If you access start.spring.io in your browser and select dependencies, a project zip will be created. We recommend the following options:
- Project: Maven
- Language: Java
- Spring Boot: 3.3.x
- Java: 21
- Dependencies: Spring Web (+ 26 H2/JPA)
2) Standard project structure
spring-boot-app/
├── pom.xml
├── src/
│ ├── main/
│ │ ├── java/com/codingnow/lecture/spring23/Application.java
│ │ └── resources/application.properties
│ └── test/
│ └── java/com/codingnow/lecture/spring23/ApplicationTests.java
3) @SpringBootApplication
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
This one annotation is a meta annotation that combines 3 (@Configuration, @EnableAutoConfiguration, @ComponentScan). Automatically finds and registers beans in the same package/subpackage.
4) Illustration of Dependency Injection (DI)
@Service
class HelloService {
String greet() { return "Hello, Spring!"; }
}
@RestController
class HelloController {
private final HelloService service;
HelloController(HelloService service) { this.service = service; }
@GetMapping("/hello") String hello() { return service.greet(); }
}
Spring creates a HelloService object and automatically passes it to the controller constructor. The core of DI is a pattern that the caller does not directly new.
5) application.properties
server.port=8081
spring.application.name=lecture-spring23
logging.level.root=INFO
See as example
Example 1 — pom.xml : Spring Boot dependency
pom.xml (Summary):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.6</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
spring-boot-starter-web One dependency includes all built-in Tomcat, Jackson (JSON), and Spring MVC.
Example 2 — Application.java: Entry point
package com.codingnow.lecture.spring23;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Example 3 — HelloService.java + HelloController.java: First case of DI
HelloService.java :
package com.codingnow.lecture.spring23;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public String greet(String name) {
return "Hello, " + name + "!";
}
}
HelloController.java :
package com.codingnow.lecture.spring23;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private final HelloService service;
public HelloController(HelloService service) {
this.service = service;
}
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "Spring") String name) {
return service.greet(name);
}
}
Example 4 — application.properties
server.port=8081
spring.application.name=lecture-spring23
run
cd 06_Spring_Boot/23_Spring_Boot_Start
mvn spring-boot:run
In another terminal:
curl http://localhost:8081/hello
# Hello, Spring!
curl "http://localhost:8081/hello?name=Jisoo"
#Hello, Jisoo!
Common Mistakes
Application.javacannot find components in other packages → Put them in the top package.- Port conflict (8080 occupied) → change
server.port - Execution does not end with
mvn spring-boot:run(exit with Ctrl+C) - Use
@Autowiredfield without constructor injection (difficult to test) - Specify Spring Boot dependency versions one by one (managed by
parent)
Cleanup
- The pair
@SpringBootApplication+mainis the entry point - DI is a pattern of “not creating objects directly”
- Environment settings with
application.properties - Can be run immediately with
mvn spring-boot:run
Try it yourself
cd 06_Spring_Boot/23_Spring_Boot_Start
mvn spring-boot:run
#another terminal
curl http://localhost:8081/hello