0% found this document useful (0 votes)
27 views3 pages

Spring Boot Annotations Guide

The document is a guide on Spring Boot annotations, detailing core, configuration, dependency injection, web, security, data JPA, transaction management, and validation annotations. Each section provides definitions and examples of how to use the annotations in a Spring Boot application. Key annotations include @SpringBootApplication, @Configuration, @RestController, and @Transactional.

Uploaded by

Aditya Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

Spring Boot Annotations Guide

The document is a guide on Spring Boot annotations, detailing core, configuration, dependency injection, web, security, data JPA, transaction management, and validation annotations. Each section provides definitions and examples of how to use the annotations in a Spring Boot application. Key annotations include @SpringBootApplication, @Configuration, @RestController, and @Transactional.

Uploaded by

Aditya Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Spring Boot Annotations Guide

1. Spring Boot Core Annotations

@SpringBootApplication
- Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan
- Used to start the Spring Boot application

Example:
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}

2. Configuration Annotations

@Configuration
- Defines configuration class

@Bean
- Declares a bean

@Value
- Injects values from properties

Example:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}

3. Dependency Injection Annotations

@Component, @Service, @Repository


- Marks class as Spring bean

@Autowired
- Automatically injects dependencies

Example:
@Service
public class UserService {}
Spring Boot Annotations Guide

4. Web Annotations (Spring MVC)

@RestController
- Combines @Controller and @ResponseBody

@RequestMapping, @GetMapping, etc.


- Maps HTTP requests

@RequestBody, @PathVariable, @RequestParam


- Bind request data

Example:
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) { ... }

5. Spring Security Annotations

@EnableWebSecurity
- Enables security configuration

@PreAuthorize, @Secured
- Method-level authorization

Example:
@PreAuthorize("hasRole('ADMIN')")
public void deleteUser(Long id) { ... }

6. Spring Data JPA Annotations

@Entity, @Id, @GeneratedValue


- JPA entity and ID generation

@OneToMany, @ManyToOne
- Entity relationships

Example:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}

7. Transaction Management

@Transactional
Spring Boot Annotations Guide

- Declares transactional method

Example:
@Transactional
public void transferMoney() { ... }

8. Validation Annotations

@NotNull, @Size, etc.


- Bean validation

@Valid
- Used in controller to trigger validation

Example:
@PostMapping("/register")
public ResponseEntity<?> register(@Valid @RequestBody User user) { ... }

You might also like