Unit 5: Spring Boot
Introduction to Spring Boot
Spring Boot is an extension of the Spring Framework that simplifies the development of stand-alone, production-ready Spring applications.
Why Spring Boot?
- Eliminates complex XML configuration
- Provides auto-configuration
- Embedded servers (Tomcat, Jetty)
- Faster development and deployment
Spring Boot Configuration
Spring Boot configuration defines how an application behaves at runtime.
Configuration Files
| File | Purpose |
|---|---|
application.properties | Key-value configuration |
application.yml | YAML-based configuration |
Example (application.properties)
Types of Configuration
- External Configuration – Properties/YAML
- Java-based Configuration –
@Configuration - Auto Configuration – Provided by Spring Boot
Spring Boot Annotations
Spring Boot uses annotations to reduce boilerplate code.
Core Spring Boot Annotations
@SpringBootApplication
Combination of:
@Configuration- @EnableAutoConfiguration
- @ComponentScan
Common Annotations
| Annotation | Purpose |
|---|---|
| @RestController | REST APIs |
| @RequestMapping | URL mapping |
| @GetMapping | GET request |
| @PostMapping | POST request |
| @Autowired | Dependency Injection |
| @Value | Inject property value |
Spring Boot Actuator
Spring Boot Actuator provides production-ready features to monitor and manage applications.
Actuator Features
- Health checks
- Metrics
- Environment info
- Application status
Common Actuator Endpoints
| Endpoint | Description |
|---|---|
/actuator/health | Application health |
/actuator/info | App info |
/actuator/metrics | Performance metrics |
/actuator/env | Environment details |
Example Dependency (Maven)
Spring Boot Build Systems
Spring Boot supports build automation tools.
Common Build Systems
| Tool | Description |
|---|---|
| Maven | XML-based |
| Gradle | Groovy/Kotlin-based |
Maven vs Gradle
| Maven | Gradle |
|---|---|
| pom.xml | build.gradle |
| Slower builds | Faster builds |
| XML syntax | Script syntax |
Spring Boot Starter Parent (Maven)
Spring Boot Code Structure
Spring Boot follows a standard project structure.
Typical Spring Boot Project Layout
Layered Architecture
| Layer | Responsibility |
|---|---|
| Controller | Handles requests |
| Service | Business logic |
| Repository | Database access |
| Model | Data objects |
Spring Boot Runners
Runners are used to execute code after application startup.
Types of Runners
1. CommandLineRunner
2. ApplicationRunner
Difference Between Runners
| CommandLineRunner | ApplicationRunner |
|---|---|
| Uses String[] args | Uses ApplicationArguments |
| Simple | More flexible |
Summary Table
| Topic | Key Concept |
|---|---|
| Spring Boot | Simplified Spring |
| Configuration | Properties/YAML |
| Annotations | Reduce code |
| Actuator | Monitoring |
| Build Systems | Maven/Gradle |
| Runners | Startup logic |
Exam Tips
- Always define Spring Boot clearly
- Write
@SpringBootApplicationcomponents - List Actuator endpoints
- Draw project structure diagram
Logger (Logging in Spring Boot)
A Logger is used to record application events, such as errors, warnings, and informational messages. Logging helps in debugging, monitoring, and maintenance of applications.
Logging Frameworks in Spring Boot
- SLF4J (default)
- Logback
- Log4j2
Common Log Levels
| Level | Use |
|---|---|
| TRACE | Detailed debugging |
| DEBUG | Debugging |
| INFO | General information |
| WARN | Warning |
| ERROR | Error |
Example: Logger in Spring Boot
Building RESTful Web Services
A RESTful Web Service is a web service that follows REST (Representational State Transfer) architecture principles and uses HTTP methods to perform operations.
REST Principles
- Stateless communication
- Resource-based URLs
- Uses standard HTTP methods
- Supports multiple data formats (JSON, XML)
Advantages of REST
- Lightweight
- Scalable
- Platform independent
- Widely used in APIs
REST Controller
@RestController
@RestController is a Spring Boot annotation used to create REST APIs.
It combines:
@Controller- @ResponseBody
Example
Request Mapping
@RequestMapping
Used to map HTTP requests to handler methods.
Example
Shortcut Annotations
| Annotation | HTTP Method |
|---|---|
| @GetMapping | GET |
| @PostMapping | POST |
| @PutMapping | PUT |
| @DeleteMapping | DELETE |
Request Body
@RequestBody
Used to read JSON data from the request body and convert it into a Java object.
Example
Use Case
-
Sending form or JSON data from frontend to backend
Path Variable
@PathVariable
Used to extract values from the URL path.
Example
Request Parameter
@RequestParam
Used to read query parameters from URL.
Example
URL Example
HTTP Methods (CRUD APIs)
GET API
-
Fetch data
POST API
-
Create new data
PUT API
-
Update existing data
DELETE API
-
Delete data
Build Web Applications using Spring Boot
Spring Boot helps in building complete web applications using:
- Spring MVC
- REST APIs
- Thymeleaf / Frontend integration
- Embedded server
Application Flow
Benefits
- Rapid development
- Easy deployment
- Scalable architecture
Comparison Table
| Concept | Purpose |
|---|---|
| Logger | Application monitoring |
| REST Controller | Build APIs |
| Request Mapping | URL handling |
| @RequestBody | Read JSON |
| @PathVariable | URL data |
| @RequestParam | Query data |
Exam Tips
- Always define REST and Logger clearly
- Write HTTP methods in CRUD format
- Mention annotations with examples
- Draw architecture flow if required