Cấu trúc dự án và phụ thuộc Maven
Một dự án Spring Boot tiêu chuẩn cần kế thừa spring-boot-starter-parent để quản lý phiên bản và cấu hình mặc định. Các starter như spring-boot-starter-web là bắt buộc nếu bạn xây dựng ứng dụng web, trong khi spring-boot-starter-test có thể thêm vào để hỗ trợ kiểm thử.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/>
</parent>
<groupId>vn.example</groupId>
<artifactId>demo-springboot-app</artifactId>
<version>1.0.0</version>
<name>Demo Spring Boot App</name>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Thiết lập Controller và lớp khởi động
Tạo một controller đơn giản để xử lý yêu cầu HTTP:
package vn.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@GetMapping("/welcome")
public String greet() {
return "<h2>Xin chào từ Spring Boot!</h2>";
}
}
Lớp chính để khởi chạy ứng dụng:
package vn.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Sau khi chạy lớp DemoApplication, truy cập http://localhost:8080/welcome để xem kết quả.
Cấu hình qua file application
Spring Boot hỗ trợ nhiều định dạng file cấu hình: .properties, .yml, và .yaml. Nếu cùng một thuộc tính được khai báo ở nhiều nơi, ưu tiên sẽ theo thứ tự: properties > yml > yaml.
Ví dụ cấu hình cổng server
- application.properties:
server.port=8081 - application.yml:
server: port: 8082 - application.yaml:
server: port: 8083
Cú pháp YAML nâng cao
Đối tượng
# Cách 1
user:
fullName: Nguyễn Văn A
# Cách 2 (inline)
user: {fullName: Nguyễn Văn A}
Mảng
# Cách 1
cities:
- Hà Nội
- Đà Nẵng
# Cách 2 (inline)
cities: [Hà Nội, Đà Nẵng]
Chuỗi và escape
text1: 'Dòng 1 \n Dòng 2' # Dấu nháy đơn: không xử lý escape
text2: "Dòng 1 \n Dòng 2" # Dấu nháy kép: xử lý escape
Tham chiếu biến
defaultName: Trần Thị B
employee:
name: ${defaultName} # Giá trị sẽ là "Trần Thị B"
Đọc cấu hình trong code
Sử dụng annotation @Value để tiêm giá trị từ file cấu hình vào biến trong lớp:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigReader {
@Value("${defaultName}")
private String userName;
@Value("${employee.name}")
private String empName;
@Value("${cities[0]}")
private String firstCity;
@Value("${text1}")
private String rawText;
@GetMapping("/config-info")
public String getConfigInfo() {
return String.format("User: %s, City: %s, Text: %s",
userName, firstCity, rawText);
}
}