RU | EN | DE

1. What is Spring Boot

  • Spring = a huge framework, requires manual configuration (XML/Java config).
  • Spring Boot = “Spring with batteries included”:
    • automatic configuration,
    • built-in Tomcat (or Jetty/Undertow),
    • starter system (dependency bundles),
    • ready-made project structure,
    • monitoring (Actuator). ⚡ You just need to write minimal code → and it already works.

2. Minimal Application

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  • @SpringBootApplication = combines three annotations:
    • @Configuration (Java Config),
    • @EnableAutoConfiguration (auto-configuration),
    • @ComponentScan (package scanning).

3. Starters (Dependencies)

Spring Boot starters = pre-built sets of dependencies.

📄 Examples (in pom.xml):

//...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
//...

📌 Popular starters:

  • spring-boot-starter-web → REST, MVC, JSON
  • spring-boot-starter-data-jpa → Hibernate + JPA
  • spring-boot-starter-security → Spring Security
  • spring-boot-starter-thymeleaf → HTML templates
  • spring-boot-starter-test → JUnit, Mockito

4. Configuration (application.properties, application.yml)

📄 src/main/resources/application.properties

server.port=8081
spring.datasource.url=jdbc:postgresql://localhost:5432/demo
spring.datasource.username=demo
spring.datasource.password=secret

📄 application.yml (more convenient for nested structures)

server:
  port: 8081
spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/demo
    username: demo
    password: secret

5. Actuator (Monitoring and Health-Check)

📄 Add the dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

📄 Settings:

management.endpoints.web.exposure.include=health,info,metrics

📌 Available endpoints:

  • /actuator/health → status
  • /actuator/info → information
  • /actuator/metrics → metrics
  • /actuator/env → environment

6. REST API (Spring Boot + MVC)

@RestController
@RequestMapping("/api/users")
class UserController {
    @GetMapping
    public List<String> getUsers() {
        return List.of("Alice", "Bob");
    }
 
    @PostMapping
    public String createUser(@RequestBody String name) {
        return "Created user: " + name;
    }
}

📌 Annotations:

  • @RestController = @Controller + @ResponseBody
  • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping
  • @PathVariable, @RequestParam, @RequestBody

7. Error Handling

@RestControllerAdvice
class GlobalExceptionHandler {
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<String> handle(IllegalArgumentException ex) {
        return ResponseEntity.badRequest().body("Error: " + ex.getMessage());
    }
}

8. Profiles (dev, test, prod)

📄 application-dev.properties

server.port=8081

📄 application-prod.properties

server.port=80

Run with a profile:

mvn spring-boot:run -Dspring-boot.run.profiles=prod

9. Logging

By default, Spring Boot uses Logback.

📄 In application.properties:

logging.level.root=INFO
logging.level.com.example=DEBUG
logging.file.name=app.log

10. Best Practices

  • ✅ Start the project using Spring Initializr
  • ✅ Use application.yml (more readable than properties).
  • ✅ Separate configurations for dev/test/prod.
  • ✅ Add Actuator for monitoring.
  • ✅ Use @RestControllerAdvice for centralized error handling.

📌 Summary of Spring Boot

  • @SpringBootApplication = entry point
  • starters = dependency bundles
  • application.yml = config
  • Actuator = monitoring
  • REST controllers = simple and fast
  • profiles = flexible configuration