


























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























































































1.3 Advantages of Using Spring Boot
1.4 Use Cases
Feature / Aspect Spring Framework Spring Boot
Setup & Configuration Manual XML/Java-based configuration
Auto-configuration, zero XML
Application Startup Requires external servlet container
Comes with embedded servers (Tomcat, Jetty, Undertow)
Dependency Management
Developer-managed dependencies
Pre-configured starter dependencies
Boilerplate Code More boilerplate and configuration
Minimal configuration and boilerplate
Project Structure Developer decides from scratch
Convention over configuration
Production Readiness Requires manual setup of monitoring tools
Built-in Actuator endpoints for health, metrics, etc.
Build Output WAR (typically) Executable JAR by default
Learning Curve Steeper for beginners Easier and quicker to get started
CLI Support No Yes, via Spring Boot CLI
Microservices Development
Needs custom setup Tailored for microservices architecture
1.5 Spring Boot Architecture
Spring Boot architecture is based on:
ā Spring Core : Base dependency injection and AOP framework ā Auto Configuration : Automatically configures beans based on classpath ā Starter Dependencies : Predefined sets of dependencies for various features (web, data, etc.) ā Spring Boot CLI : Command-line tool to run and test applications ā Embedded Server : Tomcat, Jetty, or Undertow ā Spring Boot Actuator : Provides operational endpoints for monitoring and management
4. @Configuration, @Bean 3.2. Dependency Injection (DI) 3.3. Inversion of Control (IoC)
⢠4. Configuration 4.1. application.properties vs application.yml 4.2. External Configuration (env vars, CLI args) 4.3. Profiles (@Profile) 4.4. Property Injection: @Value, @ConfigurationProperties
⢠5. Spring Boot Starters 5.1. What are Starters? 5.2. Common Starters
**1. spring-boot-starter-web
⢠6. Web Development (Spring MVC) 6.1. Creating REST APIs (@RestController) 6.2. Handling Request Parameters @PathVariable, @RequestParam, @RequestBody 6.3. ResponseEntity and Status Codes 6.4. Exception Handling @ControllerAdvice, @ExceptionHandler 6.5. Content Negotiation
⢠7. Data Access 7.1. Spring Data JPA
**1. Entity Classes (@Entity)
⢠8. Database Configuration 8.1. H2 In-Memory Database 8.2. MySQL/PostgreSQL setup 8.3. Connection Pooling (HikariCP)
8.4. Schema Initialization (schema.sql, data.sql)
⢠9. Validation 9.1. Bean Validation (javax.validation) 9.2. Using @Valid, @Validated 9.3. Custom Validators
⢠10. Security 10.1. Spring Security Basics 10.2. Authentication and Authorization 10.3. Password Encoding 10.4. JWT Token-based Security 10.5. Role-Based Access Control
⢠11. Testing 11.1. Unit Testing with JUnit 11.2. Mocking with Mockito 11.3. Integration Testing 11.4. Web Layer Testing (@WebMvcTest) 11.5. Data Layer Testing (@DataJpaTest)
⢠12. Logging 12.1. SLF4J and Logback 12.2. Configuring Log Levels 12.3. External Log Configuration
⢠13. Actuator & Monitoring 13.1. Spring Boot Actuator Overview 13.2. Common Actuator Endpoints 13.3. Custom Metrics 13.4. Prometheus/Grafana Integration
⢠14. Error Handling 14.1. Default Error Handling 14.2. Custom Error Pages 14.3. Global Exception Handling
⢠15. Developer Tools 15.1. Spring Boot DevTools 15.2. Auto Restart 15.3. Live Reload
2.1. Setting up Spring Boot Project (Spring Initializr,
Maven, Gradle)
https://start.spring.io/
2.2. Project Structure Overview
2.3. Application Entry Point (@SpringBootApplication)
3. Core Concepts
3.1. Spring Boot Annotations
The @SpringBootApplication annotation is the primary entry point of any Spring Boot application. It is a convenience annotation that combines three commonly used annotations in Spring:
@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
Declaration
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Definition
@Component is a generic stereotype annotation that marks a class as a Spring-managed component.
@Component
public class MyComponent {
// Business logic
}
Use Case
Used when the class doesn't clearly fall into service, repository, or controller layers.
Definition
@Service is a specialization of @Component used to annotate service layer classes that contain business logic.
@Service
public class OrderService {
public void processOrder() {
// Business logic
}
}
Use Case
ā Encapsulates business logic
ā Makes the code more readable and structured
ā Can integrate with AOP (for transaction, logging, etc.)
Definition
@Repository is a specialization of @Component used for DAO (Data Access Object) classes. It also provides exception translation for database errors.
Definition
@RestController = @Controller + @ResponseBody Used to define RESTful web services. The return values are serialized as JSON or XML and sent directly to the client.
@RestController
public class UserController {
@GetMapping("/users")
public List getAllUsers() {
return userService.getAll();
}
}
Use Case
ā Used in REST APIs
ā Automatically serializes Java objects to JSON
Annotation Layer Inherits From Purpose Returns Response Body
@Component Generic^ -^ General-purpose component
No
@Service Service Layer
@Component Business logic and services
No
@Repository Data Layer^ @Component DAO & exception translation
No
@Controller Web (MVC)^ @Component Handles web requests & returns views
No (uses View Resolver)
@RestControl ler
Web (REST API)
@Controlle r
Returns JSON/XML directly to the client
Yes
@Autowired is used to automatically wire dependencies.
@Qualifier is used to resolve conflicts when multiple beans of the same type are available.
Definition
@Autowired is used to automatically inject a bean from the Spring container into another bean by type.
@Component
public class CarService {
@Autowired
private Engine engine;
// engine will be injected automatically
}
Where It Can Be Used
ā On fields
public class PetrolEngine implements Engine { }
@Service
public class CarService {
@Autowired
@Qualifier("dieselEngine")
private Engine engine;
}
Use Case
ā Disambiguating among multiple beans of the same type.
ā Explicitly specifying the bean to be injected.
Definition
@Configuration is a class-level annotation that indicates the class contains bean definitions for the Spring IoC container. It serves as a replacement for XML-based configuration.
@Configuration
public class AppConfig {
// Bean definitions go here
Definition