Skip to content

Latest commit

 

History

History
611 lines (457 loc) · 14.8 KB

SpringBootAnnotations.md

File metadata and controls

611 lines (457 loc) · 14.8 KB

Understanding Spring Boot Annotations

Spring Boot is a powerful framework that simplifies the development of Spring-based applications. It provides a set of tools and features to make application development faster and more efficient. One of the key aspects of Spring Boot is its use of annotations, which make configuration and development easier.

Table of Contents

Introduction to Spring and Spring Boot

Spring Framework is a comprehensive framework for enterprise Java development. It provides support for developing Java applications with a wide range of features, including dependency injection, transaction management, and aspect-oriented programming.

Spring Boot builds on top of the Spring Framework and simplifies the process of creating production-ready applications. It offers a range of features such as auto-configuration, embedded servers, and simplified dependency management.

Advantages of Using Spring Boot

  • Simplified Configuration: Spring Boot's auto-configuration capabilities reduce the need for manual configuration.
  • Embedded Servers: It supports embedded servers like Tomcat, Jetty, and Undertow, allowing applications to run independently.
  • Production-Ready Features: Built-in features for monitoring, metrics, and health checks are available.
  • Rapid Development: It speeds up the development process with its starter templates and extensive documentation.

Spring Boot Annotations

Core Annotations

@SpringBootApplication

This annotation is the entry point for a Spring Boot application. It combines @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations to enable a Spring Boot application with minimal configuration.

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

Explanation:

  • @Configuration: Marks the class as a source of bean definitions.
  • @EnableAutoConfiguration: Enables Spring Boot’s auto-configuration feature.
  • @ComponentScan: Scans for Spring components in the specified package.

@Configuration

Used to define configuration classes. These classes are responsible for creating and configuring Spring beans.

@Configuration
public class MyConfiguration {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

Explanation:

  • Defines a class as a source of bean definitions.
  • @Bean: Marks a method as a bean producer.

@ComponentScan

This annotation tells Spring where to search for components, services, and repositories.

@SpringBootApplication
@ComponentScan(basePackages = "com.example")
public class MySpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }
}

Explanation:

  • Scans the specified package for components to register as beans.

@Bean

Declares a method as a Spring bean, which will be managed by the Spring container.

@Configuration
public class MyConfiguration {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

Explanation:

  • The annotated method returns an object that should be registered as a bean.

@Component

Marks a class as a Spring component, so it can be automatically detected and registered as a bean.

@Component
class ProductService {
    // Class definition
}

Explanation:

  • Indicates that the class is a Spring-managed component.

Dependency Injection

@Autowired

Used for automatic dependency injection. It allows Spring to resolve and inject collaborating beans into your bean.

@Service
public class MyService {
    private final MyRepository myRepository;

    @Autowired
    public MyService(MyRepository myRepository) {
        this.myRepository = myRepository;
    }
}

Explanation:

  • Automatically injects the required dependencies.

@Qualifier

Disambiguates between multiple beans of the same type. It is used along with @Autowired to specify which bean to inject.

@Controller
public class MyController {
    @Autowired
    @Qualifier("myRepositoryImpl")
    private MyRepository myRepository;
}

Explanation:

  • Specifies which bean to use when multiple beans of the same type are available.

Web Annotations

@Controller

Marks a class as a Spring MVC controller. It is used to handle HTTP requests.

@Controller
public class MyController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

Explanation:

  • Indicates that the class is a web controller.

@ResponseBody

Used to indicate that the return value of a method should be bound to the web response body.

@Controller
public class MyController {
    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "Hello, World!";
    }
}

Explanation:

  • The return value is sent directly as the response body, bypassing view resolution.

@RestController

A convenience annotation that combines @Controller and @ResponseBody, making it easier to create RESTful web services.

@RestController
public class MyRestController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

Explanation:

  • Simplifies the creation of RESTful endpoints by combining @Controller and @ResponseBody.

@RequestMapping

Maps HTTP requests to handler methods of MVC and REST controllers. It can be used at both the class and method level.

@Controller
public class MyController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

Explanation:

  • Handles HTTP requests by mapping them to methods.

@GetMapping, @PostMapping, @PutMapping, @DeleteMapping

Shortcuts for @RequestMapping with specific HTTP methods. They simplify the creation of RESTful API endpoints.

@RestController
@RequestMapping("/api/products")
public class ProductController {
    
    @GetMapping("/{id}")
    public ResponseEntity<Product> getProductById(@PathVariable Long id) {
        // Implementation
    }
    
    @PostMapping
    public ResponseEntity<Product> createProduct(@RequestBody Product product) {
        // Implementation
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<Product> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        // Implementation
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
        // Implementation
    }
}

Explanation:

  • @GetMapping: Handles GET requests.
  • @PostMapping: Handles POST requests.
  • @PutMapping: Handles PUT requests.
  • @DeleteMapping: Handles DELETE requests.

Spring Data JPA

@Entity

Specifies that the class is an entity and is mapped to a database table.

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String name;
    private String email;
    
    // Getters and setters
}

Explanation:

  • Marks the class as an entity.
  • @Id: Specifies the primary key.
  • @GeneratedValue: Provides the strategy for primary key generation.

@Table

Specifies the table name in the database to which the entity is mapped.

@Entity
@Table(name = "users")
public class User {
    // Fields and methods
}

Explanation:

  • Maps the entity to the specified table.

@Id

Indicates the primary key of an entity.

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    // Other fields and methods
}

Explanation:

  • Marks the field as the primary key.

@GeneratedValue

Provides the generation strategy for the primary key.

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    // Other fields and methods
}

Explanation:

  • Specifies how the primary key should be generated.

@Repository

Indicates that the class is a Spring Data repository. It is used to encapsulate storage, retrieval, and search behavior.

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    // Custom query methods
}

Explanation:

  • Marks the interface as a repository.

Validation

@Valid

Used to validate the request body or method parameters.

@RestController
@RequestMapping("/api/users")
public class UserController {
    
    @PostMapping
    public ResponseEntity<User> createUser(@Valid @RequestBody User user) {
        // Implementation
    }
}

Explanation:

  • Triggers validation on the annotated parameter.

@NotNull, @Size, @Min, @Max, @Pattern

Used for validating fields in an entity.

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @NotNull
    private String name;
    
    @Size(min = 5, max = 50)
    private String email;
    
    @Min(18)
    @Max(100)
    private Integer age;
    
    @Pattern(regexp = "\\d{10}")
    private String phoneNumber;
    
    // Getters and setters
}

Explanation:

  • @NotNull: Ensures the field is not null.
  • @Size: Specifies the size constraints.
  • @Min: Specifies the minimum value.
  • @Max: Specifies the maximum value.
  • @Pattern: Validates against the specified regex pattern.

Spring Security

@EnableWebSecurity

Enables Spring Security’s web security support and provides the Spring MVC integration.

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // Security configuration
}

Explanation:

  • Enables web security.

@PreAuthorize, @PostAuthorize

Used to define method-level security. @PreAuthorize checks the authorization before entering the method, while @PostAuthorize checks the authorization after the method has been executed.

@RestController
@RequestMapping("/api/users")
public class UserController {
    
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/{id}")
    public ResponseEntity<User> getUserById(@PathVariable Long id) {
        // Implementation
    }
    
    @PostAuthorize("returnObject.owner == principal.username")
    @GetMapping("/profile")
    public ResponseEntity<User> getProfile() {
        // Implementation
    }
}

Explanation:

  • @PreAuthorize: Checks the security before the method execution.
  • @PostAuthorize: Checks the security after the method execution.

Testing

@SpringBootTest

Used for integration tests. It loads the complete Spring application context.

@SpringBootTest
public class UserServiceTests {
    
    @Autowired
    private UserService userService;
    
    @Test
    public void testCreateUser() {
        // Test implementation
    }
}

Explanation:

  • Loads the entire application context for integration testing.

@MockBean

Used to add mock beans to the Spring application context.

@SpringBootTest
public class UserServiceTests {
    
    @MockBean
    private UserRepository userRepository;
    
    @Autowired
    private UserService userService;
    
    @Test
    public void testCreateUser() {
        // Test implementation
    }
}

Explanation:

  • Adds a mock bean to the application context.

@Test

Indicates that a method is a test method.

@SpringBootTest
public class UserServiceTests {
    
    @Test
    public void testCreateUser() {
        // Test implementation
    }
}

Explanation:

  • Marks the method as a test method.

Logging

@Slf4j

A Lombok annotation to create a logger field.

@Slf4j
public class MyService {
    public void performAction() {
        log.info("Action performed");
    }
}

Explanation:

  • Creates a logger field using Lombok.

Exception Handling

@ControllerAdvice

A specialization of @Component for global exception handling.

@ControllerAdvice
public class GlobalExceptionHandler {
    
    @ExceptionHandler(UserNotFoundException.class)
    public ResponseEntity<String> handleUserNotFound(UserNotFoundException ex) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
    }
}

Explanation:

  • Provides global exception handling.

@ExceptionHandler

Used to handle exceptions in specific handler methods or global handler methods.

@ControllerAdvice
public class GlobalExceptionHandler {
    
    @ExceptionHandler(UserNotFoundException.class)
    public ResponseEntity<String> handleUserNotFound(UserNotFoundException ex) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
    }
}

Explanation:

  • Handles specific exceptions.

Additional Resources