Spring Boot Annotations Cheat Sheet
1. Core Spring Boot Annotations
@SpringBootApplication
- Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.
@Configuration
- Defines a class as a Spring configuration class.
@ComponentScan
- Scans for Spring components (e.g., @Component, @Service, @Repository).
@Bean
- Declares a Spring-managed bean inside a @Configuration class.
@PropertySource
- Loads properties from external files into Springs Environment.
2. Component Scanning and Dependency Injection
@Component
- Marks a class as a Spring component.
@Service
- Specialized @Component for service-layer beans.
@Repository
- Specialized @Component for DAO layer.
@Autowired
- Injects a bean automatically.
@Qualifier
- Specifies which bean to inject when multiple exist.
@Primary
- Marks a bean as the primary choice for dependency injection.
3. Web and REST API Annotations
@RequestMapping
- Maps HTTP requests to handler methods.
@GetMapping
- Handles GET requests.
@PostMapping
- Handles POST requests.
@PutMapping
- Handles PUT requests.
@DeleteMapping
- Handles DELETE requests.
@RequestParam
- Extracts query parameters.
@PathVariable
- Extracts a URI path variable.
@RequestBody
- Converts request JSON to Java object.
@ResponseBody
- Converts Java object to JSON response.
4. Data Access and JPA Annotations
@Entity
- Marks a class as a JPA entity.
@Table(name='table_name')
- Specifies the table name for an entity.
@Id
- Marks a field as the primary key.
@GeneratedValue
- Auto-generates primary key values.
@Column(name='column_name')
- Maps an entity field to a database column.
@OneToOne / @OneToMany / @ManyToOne / @ManyToMany
- Defines relationships between entities.
5. Spring Boot Security Annotations
@EnableWebSecurity
- Enables Spring Security.
@PreAuthorize
- Restricts access based on roles.
@Secured
- Restricts access to specific roles.
@RolesAllowed
- Restricts method access to specified roles.
6. Transaction Management Annotations
@Transactional
- Defines a transaction boundary for methods or classes.
@Rollback
- Rolls back transactions in test cases.
@EnableTransactionManagement
- Enables transaction management in Spring Boot.
7. Spring Boot Testing Annotations
@SpringBootTest
- Loads the complete Spring Boot application context for testing.
@WebMvcTest
- Loads only the web layer for testing controllers.
@DataJpaTest
- Loads only the JPA-related components for database testing.
@MockBean
- Mocks a Spring bean for testing.
8. Spring Boot Actuator & Monitoring
@EnableAutoConfiguration
- Enables automatic configuration.
@ConditionalOnClass
- Configures beans only if a specific class is present.
@ConditionalOnProperty
- Configures beans based on property values.
@RefreshScope
- Enables runtime property refresh.