SpringBoot_Annotations_Presentation_Refined.pptx

ashutoshkumar3640 17 views 21 slides Oct 17, 2024
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation


Slide Content

Introduction to Spring Boot Annotations Annotations are metadata providing instructions to the compiler and runtime. Spring Boot processes annotations during component scanning and runtime. Techniques used: Reflection, Proxy creation, and Dependency Injection.

@SpringBootApplication Purpose: Marks the main class of a Spring Boot application. How it Works: Combines @Configuration, @EnableAutoConfiguration, @ComponentScan. Code Example: @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } Use Case: Entry point for Spring Boot applications.

@RestController Purpose: Marks a class as a REST controller for HTTP requests. How it Works: Combines @Controller and @ResponseBody to return JSON or XML. Code Example: @RestController @GetMapping("/hello") public String hello() { return "Hello!"; } Use Case: Creating REST APIs.

@Controller Purpose: Defines a class as a web controller. How it Works: Returns views like HTML templates. Code Example: @Controller public String home() { return "home"; } Use Case: Handling traditional web views.

@RequestMapping Purpose: Maps HTTP requests to methods. How it Works: Supports GET, POST, etc. Code Example: @RequestMapping("/api") public String api() { return "API Endpoint"; } Use Case: Organizing routes.

@GetMapping, @PostMapping, etc. Purpose: Shortcut for HTTP methods. How it Works: Provides specific HTTP routes. Code Example: @GetMapping("/items") public List<String> getItems() { return List.of("Item1", "Item2"); } Use Case: Handling CRUD operations.

@Autowired Purpose: Injects dependencies automatically. How it Works: Supports field, constructor, or setter injection. Code Example: @Autowired private UserService userService; Use Case: Injecting services or components.

@Service Purpose: Marks a class as a service component. How it Works: Automatically registered as a Spring bean. Code Example: @Service public class UserService { } Use Case: Business logic components.

@Component Purpose: Marks a class as a Spring-managed component. How it Works: Included in component scanning. Code Example: @Component public class UtilityClass { } Use Case: Defining reusable components.

@Repository Purpose: Identifies data access components. How it Works: Manages transactions and persistence logic. Code Example: @Repository public interface UserRepository extends JpaRepository<User, Long> { } Use Case: Database interaction.

@Configuration Purpose: Declares a class as a source of bean definitions. How it Works: Uses @Bean methods for bean creation. Code Example: @Configuration public class AppConfig { @Bean public Service service() { return new Service(); } } Use Case: Centralized bean configuration.

@Bean Purpose: Registers a method's return value as a bean. How it Works: Creates beans manually. Code Example: @Bean public Service service() { return new Service(); } Use Case: Programmatically defining beans.

@Value Purpose: Injects values from configuration files. How it Works: Reads properties from application.properties. Code Example: @Value("${app.name}") private String appName; Use Case: External configuration loading.

@PathVariable Purpose: Binds URL path parameters. How it Works: Extracts variables from URLs. Code Example: @GetMapping("/users/{id}") public User getUser(@PathVariable Long id) { return userService.findById(id); } Use Case: Handling dynamic URLs.

@RequestParam Purpose: Binds query parameters. How it Works: Reads query strings from URLs. Code Example: @GetMapping("/search") public String search(@RequestParam String q) { return "Searching for " + q; } Use Case: Reading query parameters.

@ResponseBody Purpose: Sends method return as response body. How it Works: Converts objects into JSON or XML. Code Example: @ResponseBody public String getData() { return "Hello, World!"; } Use Case: Returning JSON responses.

@RequestBody Purpose: Maps HTTP body to method parameter. How it Works: Reads input as JSON or XML. Code Example: @PostMapping("/users") public void createUser(@RequestBody User user) { userService.save(user); } Use Case: Handling JSON input.

@Transactional Purpose: Manages transactions automatically. How it Works: Commits or rolls back transactions. Code Example: @Transactional public void updateUser(User user) { userRepository.save(user); } Use Case: Managing database transactions.

@CrossOrigin Purpose: Enables Cross-Origin requests. How it Works: Allows requests from other domains. Code Example: @CrossOrigin @GetMapping("/public") public String publicEndpoint() { return "Public Data"; } Use Case: Handling CORS in web apps.

@Profile Purpose: Activates beans for specific profiles. How it Works: Loads beans based on active profiles. Code Example: @Profile("dev") @Bean public DataSource dataSource() { return new H2DataSource(); } Use Case: Environment-specific configurations.

Conclusion Spring Boot annotations simplify configuration and enable dependency injection. They reduce boilerplate and enhance maintainability. Crucial for building modern, scalable applications.
Tags