Mastering Spring Boot Core Annotations: A Complete Guide with Examples

2 min read

Mastering Spring Boot Core Annotations: A Complete Guide with Examples

1. Introduction

Spring Boot simplifies Java application development by reducing boilerplate code and providing a powerful set of annotations to configure applications efficiently. Among these, the core annotations form the backbone of how Spring Boot manages configuration, dependency injection, and bean creation. This article explores the most fundamental core annotations used in Spring Boot and demonstrates their usage in a practical example.

2. Problem

Developers often struggle with manually configuring beans, managing dependencies, and wiring components together. Before Spring Boot, these tasks typically required verbose XML configurations and complex setup files. This manual process not only slowed down development but also introduced errors and made applications harder to maintain.

The challenge, therefore, is to find a clean, declarative way to configure and wire application components with minimal configuration overhead.

3. Solution

Spring Boot introduces a set of core annotations that handle configuration, bean management, and dependency injection seamlessly. These annotations allow developers to define relationships and dependencies directly within Java classes, eliminating the need for XML configuration.

  • @SpringBootApplication
  • @Configuration
  • @Bean
  • @Component
  • @Value

By using these annotations, developers can create modular, maintainable, and testable applications with clear component boundaries.

4. Implementation

In this section, we will demonstrate how to use core annotations in a simple Spring Boot project that greets users.

Step 1: Create the main application class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Explanation:
@SpringBootApplication is a composite annotation that combines three critical annotations:

  • @Configuration: Marks this class as a source of bean definitions.
  • @EnableAutoConfiguration: Enables automatic configuration based on dependencies.
  • @ComponentScan: Automatically scans for components in the package and its subpackages.

Step 2: Create a configuration class

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public GreetingService greetingService() {
        return new GreetingService();
    }
}

Explanation:
@Configuration marks the class as a configuration source.
@Bean declares a bean to be managed by the Spring container. The method name becomes the bean name by default.

Step 3: Define a component class

import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;

@Component
public class MessageProvider {

    @Value("${app.greeting.message:Hello}")
    private String message;

    public String getMessage() {
        return message;
    }
}

Explanation:
@Component makes the class automatically detected and managed by Spring’s component scanner.
@Value injects a value from the application properties. The default value “Hello” is used if the property is not found.

Step 4: Create a service class

public class GreetingService {

    private final MessageProvider messageProvider;

    public GreetingService(MessageProvider messageProvider) {
        this.messageProvider = messageProvider;
    }

    public String greet(String name) {
        return messageProvider.getMessage() + ", " + name + "!";
    }
}

This service class uses constructor injection to obtain a reference to MessageProvider. Since both are managed by Spring, dependencies are injected automatically.

Step 5: Create a REST controller to test the service

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private final GreetingService greetingService;

    public GreetingController(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    @GetMapping("/greet")
    public String greet(@RequestParam(defaultValue = "World") String name) {
        return greetingService.greet(name);
    }
}

Run the Application:
Start the application and open your browser to:

http://localhost:8080/greet?name=Spring

Expected output:

Hello, Spring!

5. Conclusion

Core annotations are the foundation of every Spring Boot application. They provide a concise, declarative way to define beans, configure dependencies, and manage application setup without the need for XML. By using @SpringBootApplication, @Configuration, @Bean, @Component, and @Value, developers can focus on writing business logic while letting Spring handle configuration details. Mastering these annotations leads to cleaner, more maintainable, and highly scalable Java applications.

🤞 Never miss a story from us, get weekly updates to your inbox!

Leave a Reply

Your email address will not be published. Required fields are marked *