Spring Boot Annotations

An annotation is a small label that starts with the @ symbol. Developers place labels above classes, methods, and fields to tell Spring Boot what each piece of code does. Spring reads these labels at startup and builds the application around them.

Annotations replace long configuration files. One short label can create an object, connect two classes, or open a web address. Learning the most common labels unlocks almost every Spring Boot project.

The Warehouse Label Idea

Imagine a warehouse full of boxes. Workers read the sticker on each box and know the action instantly: "Fragile" means handle gently, "Cold" means move to the fridge, "Express" means ship today. Spring Boot behaves like that worker. The framework scans every class, reads its sticker, and acts without extra instructions.

 +--------------------+        +----------------+        +------------------+
 | @Service           |  scan  |  Spring Boot   |  act   | Object created   |
 | class OrderService | -----> |  reads labels  | -----> | and managed      |
 +--------------------+        +----------------+        +------------------+

Starting the Application

Every Spring Boot project begins with @SpringBootApplication. The label sits on the main class and combines three smaller labels.

Included LabelJob
@ConfigurationMarks the class as a source of bean definitions
@EnableAutoConfigurationTurns on automatic setup based on the classpath
@ComponentScanSearches the package and its children for labelled classes
@SpringBootApplication
public class ShopApplication {
    public static void main(String[] args) {
        SpringApplication.run(ShopApplication.class, args);
    }
}

Stereotype Annotations

Stereotype labels mark a class as a Spring-managed object and describe its role in the application.

LabelRole
@ComponentGeneral-purpose managed class
@ServiceBusiness logic layer
@RepositoryDatabase access layer
@ControllerWeb layer that returns pages
@RestControllerWeb layer that returns data such as JSON

All five labels create managed objects. Choosing the specific label documents the purpose of the class and lets Spring add layer-specific features, such as translating database errors in repositories.

Injection Annotations

Injection labels connect classes to each other.

  • @Autowired asks Spring to supply a matching object.
  • @Qualifier picks one object by name when several candidates exist.
  • @Value reads a single value from application.properties.
@Service
public class EmailService {

    private final MailSender sender;

    @Value("${shop.support.email}")
    private String supportEmail;

    public EmailService(MailSender sender) {
        this.sender = sender;
    }
}

Constructor injection, shown above, needs no @Autowired label when a class has a single constructor.

Configuration Annotations

The @Configuration label marks a class that builds objects by hand. Methods inside it carry the @Bean label, and Spring stores each returned object for later use. Use this approach for classes from outside libraries, because their source code cannot receive stereotype labels.

@Configuration
public class AppConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

Web Annotations

Web labels turn Java methods into web addresses.

LabelMeaning
@GetMappingHandles HTTP GET requests
@PostMappingHandles HTTP POST requests
@PathVariableReads a value from the address path
@RequestParamReads a query string value
@RequestBodyConverts incoming JSON to a Java object

Data Annotations

Database labels describe how a Java class maps to a table.

@Entity
@Table(name = "products")
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;
}

@Entity declares the class as a table row, @Id marks the primary key, and @Column adjusts one field.

Putting Labels Together

 @RestController  -->  receives request
        |
        v
 @Service         -->  applies business rules
        |
        v
 @Repository      -->  talks to database
        |
        v
 @Entity          -->  represents the table row

Best Practices

  • Pick the most specific stereotype label for every class.
  • Prefer constructor injection over field injection for easier testing.
  • Keep labels close to the code they describe.
  • Read the official Javadoc when a label seems unclear.
  • Avoid stacking unrelated labels on one class. A class with ten labels usually does too many jobs.

Annotations turn plain Java classes into a working application with very little code. Reading a class becomes quick, because the labels announce its purpose at a glance.