Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring Boot Annotations

Spring Boot provides a variety of annotations to help developers quickly bootstrap applications, define components, and configure properties. These annotations play an essential role in reducing boilerplate code and allowing developers to focus on business logic.

Here's a rundown of some commonly used Spring Boot annotations:

1. Main/Application Annotations:

  • @SpringBootApplication: A convenience annotation that encompasses @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan. This is typically used on the main application class.

2. Component Annotations:

  • @Component: Generic stereotype for any Spring-managed component.
  • @Service: Stereotype for service classes, holding business logic.
  • @Repository: Stereotype for data access components (e.g., DAOs).
  • @Controller: Stereotype for MVC controller classes in a web application.
  • @RestController: A combination of @Controller and @ResponseBody, which indicates that the returned data should be written directly to the HTTP response as JSON or XML.

3. Configuration Annotations:

  • @Configuration: Indicates that the class has @Bean definition methods. It can be used to create Spring beans programmatically.
  • @EnableAutoConfiguration: Enables Spring Boot's auto-configuration mechanism.
  • @ConfigurationProperties: Allows binding and validation of external configurations (e.g., properties or YAML files) to a Java object.
  • @Value: Annotation at the field level to inject values from properties files.

4. Web Annotations:

  • @RequestMapping: Annotation for mapping web requests to specific handler methods or classes.
  • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, etc.: Shortcuts for @RequestMapping with specific HTTP methods.
  • @PathVariable: Indicates that a method parameter should be bound to a URI template variable.
  • @RequestParam: Indicates that a method parameter should be bound to a value of a web request parameter.
  • @RequestBody: Indicates that a method parameter should be bound to the body of the HTTP request.

5. Data/JPA Annotations:

  • @Entity: Specifies that the class is an entity and will have a corresponding table in the database.
  • @Table: Provides additional information about the entity's table.
  • @Id: Specifies the primary key of the entity.
  • @GeneratedValue: Provides generation strategies for primary keys.
  • @Column: Provides additional settings for entity attributes, like nullable or unique.

6. Aspect-Oriented Annotations:

  • @Aspect: Indicates that a class is an "aspect", holding pointcuts, advices, etc.
  • @Before, @After, @Around, etc.: Annotations to denote advice types in aspect-oriented programming.

7. Miscellaneous:

  • @Autowired: Indicates that a bean should be injected from the Spring context. Can be used on fields, setters, or constructors.
  • @Qualifier: Used along with @Autowired to specify which bean should be injected when there are multiple candidates.
  • @Profile: Indicates that a bean or configuration should be active only in specific profiles.

This is not an exhaustive list, as Spring Boot and its related projects offer a wide range of annotations to cater to various needs. However, these are some of the commonly used annotations that developers will encounter while working with Spring Boot.

  1. Spring Boot @Autowired annotation:

    • Used for automatic dependency injection.
    • Applied to fields, constructors, or methods.
    • Example:
      @RestController
      public class MyController {
          @Autowired
          private MyService myService;
      }
      
  2. Transaction management with @Transactional in Spring Boot:

    • @Transactional: Annotate methods to enable transaction management.
    • Ensures atomicity, consistency, isolation, and durability (ACID) properties.
    • Example:
      @Service
      public class MyService {
          @Autowired
          private MyRepository myRepository;
      
          @Transactional
          public void performTransaction() {
              // Transactional logic
          }
      }
      
  3. Security annotations in Spring Boot:

    • Spring Security provides various annotations.
    • @Secured: Restricts access based on roles.
    • @PreAuthorize, @PostAuthorize: Expression-based access control.
    • Example:
      @Secured("ROLE_USER")
      @PreAuthorize("hasRole('ADMIN') and authentication.name == 'admin'")
      
  4. Custom annotations in Spring Boot:

    • Create custom annotations for specific behaviors.
    • Example:
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.METHOD)
      public @interface CustomAnnotation {
          // Annotation attributes
      }