Spring Boot Annotations: A Complete Guide with Examples
This document provides a comprehensive guide to Spring Boot annotations, including definitions,
explanations, examples, and use cases. It is organized to help developers understand why, where,
when, and how each annotation is used in a Spring Boot application.
1. Basic Annotations
@SpringBootApplication
Marks the main application class and combines @Configuration, @EnableAutoConfiguration, and
@ComponentScan for simplified setup.
Example:
Example:
import [Link];
import [Link];
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
[Link]([Link], args);
@Component, @Service, @Repository
@Component is a general-purpose bean annotation, while @Service and @Repository specify
service and repository layers, respectively.
Example:
Example:
import [Link];
@Service
public class UserService {
public String getUser() {
return "User details";
}
2. Configuration Annotations
@Configuration
Marks a class as a source of bean definitions. Used with @Bean to define Spring beans.
Example:
Example:
import [Link];
import [Link];
@Configuration
public class AppConfig {
@Bean
public UserService userService() {
return new UserService();
@Bean
Indicates a method that returns a Spring bean, usually within a @Configuration class.
Example:
Example:
@Bean
public UserService userService() {
return new UserService();
3. Web Layer Annotations
@RestController
Indicates that a class handles RESTful requests, automatically serializing responses to JSON.
Example:
Example:
import [Link];
import [Link];
@RestController
public class UserController {
@GetMapping("/user")
public String getUser() {
return "User details";
@RequestMapping, @GetMapping, @PostMapping
Map HTTP requests to controller methods. @RequestMapping is general, while others specify
HTTP methods.
Example:
Example:
@GetMapping("/hello")
public String sayHello() {
return "Hello, world!";
4. Data Layer Annotations
@Entity
Defines a JPA entity, mapping the class to a database table.
Example:
Example:
import [Link];
import [Link];
@Entity
public class User {
@Id
private Long id;
private String name;
@Id
Marks a field as the primary key in a JPA entity.
Example:
Example:
@Id
private Long id;
5. Transactional and Security Annotations
@Transactional
Manages transactions, ensuring data consistency across multiple operations.
Example:
Example:
import [Link];
@Transactional
public void saveUser(User user) {
// logic to save user
@PreAuthorize, @Secured
Used for role-based access control in methods.
Example:
Example:
import [Link];
@PreAuthorize("hasRole('ROLE_ADMIN')")
public void adminOnlyTask() {
// admin task
6. Testing Annotations
@SpringBootTest
Loads the Spring application context for integration testing.
Example:
Example:
import [Link];
import [Link];
@SpringBootTest
public class ApplicationTests {
@Test
public void contextLoads() {
@MockBean
Creates a mock bean for testing, replacing the actual bean in the application context.
Example:
Example:
import [Link];
@MockBean
private UserService userService;
7. Advanced Annotations
@EnableAutoConfiguration
Enables automatic configuration, allowing Spring Boot to set up beans based on classpath settings.
Example:
Example:
import [Link];
@EnableAutoConfiguration
public class MyAppConfig {}
@ConditionalOnProperty
Defines conditions for bean creation based on properties.
Example:
Example:
import [Link];
@Bean
@ConditionalOnProperty(name = "[Link]", havingValue = "true")
public FeatureService featureService() {
return new FeatureService();