Spring Boot Annotations – Detailed Explanation
Spring Boot annotations are metadata that provide instructions to the Spring framework on how to configure, initialize, and manage application components. They minimize configuration effort and improve development speed.
1. Core Spring Boot Annotations
1.1 @SpringBootApplication
This is the main annotation used to mark a Spring Boot application.
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Internally it combines:
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
1.2 @EnableAutoConfiguration
Automatically configures beans based on:
- Classpath dependencies
- Existing beans
- Application properties
Example: If spring-boot-starter-web is present, Spring Boot automatically configures Tomcat and DispatcherServlet.
1.3 @ComponentScan
Used to scan and register Spring components.
@ComponentScan(basePackages = "com.example")
It detects:
- @Component
- @Service
- @Repository
- @Controller
2. Stereotype Annotations
2.1 @Component
A generic Spring-managed bean.
@Component
public class EmailUtil {
}
2.2 @Service
Represents the service layer where business logic is written.
@Service
public class OrderService {
}
2.3 @Repository
Used for DAO layer and database interaction.
@Repository
public class UserRepository {
}
It translates database-specific exceptions into Spring’s DataAccessException.
2.4 @Controller
Used in Spring MVC applications to return views.
@Controller
public class PageController {
}
2.5 @RestController
Used for RESTful web services.
@RestController
public class UserController {
}
It is a combination of @Controller and @ResponseBody.
3. Dependency Injection Annotations
3.1 @Autowired
Injects dependent objects automatically.
@Autowired
private UserService userService;
Types of injection:
- Field Injection
- Constructor Injection (Recommended)
- Setter Injection
3.2 @Qualifier
Used when multiple beans of the same type exist.
@Autowired
@Qualifier("mysqlDB")
private DatabaseService dbService;
3.3 @Primary
Marks a default bean when multiple beans are available.
@Primary
@Service
public class OracleDBService implements DatabaseService {
}
4. Configuration & Property Annotations
4.1 @Configuration
Defines a configuration class.
@Configuration
public class AppConfig {
}
4.2 @Bean
Used to create and configure a Spring bean manually.
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
4.3 @Value
Injects values from application.properties.
@Value("${server.port}")
private int port;
4.4 @ConfigurationProperties
Used to bind a group of related properties.
@ConfigurationProperties(prefix = "db")
public class DBConfig {
private String url;
private String username;
}
5. Web & REST Annotations
5.1 @RequestMapping
Maps HTTP requests to handler methods.
@RequestMapping("/api/users")
5.2 HTTP Method Annotations
@GetMapping("/users")
@PostMapping("/users")
@PutMapping("/users/{id}")
@DeleteMapping("/users/{id}")
5.3 @PathVariable
Extracts values from the URL path.
@GetMapping("/users/{id}")
public User getUser(@PathVariable int id) {
}
5.4 @RequestParam
Extracts query parameters from URL.
@GetMapping("/users")
public List getUsers(@RequestParam int page) {
}
5.5 @RequestBody
Maps HTTP request body to a Java object.
@PostMapping("/users")
public User save(@RequestBody User user) {
}
5.6 @ResponseBody
Returns data directly in the response body.
@ResponseBody
public String hello() {
return "Hello";
}
6. JPA & Database Annotations
@Entity
@Entity
public class User {
}
@Id & @GeneratedValue
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@ManyToOne
@JoinColumn(name = "dept_id")
private Department department;
7. Transaction & AOP Annotations
@Transactional
Manages database transactions automatically.
@Transactional
public void saveOrder() {
}
8. Validation Annotations
@NotNull
@NotEmpty
@NotBlank
@Size(min = 5, max = 10)
@Email
9. Scheduling & Async Annotations
@Scheduled
@Scheduled(fixedRate = 5000)
public void runTask() {
}
@Async
@Async
public void sendEmail() {
}
10. Testing Annotations
@SpringBootTest
Loads full application context for integration testing.
@MockBean
@MockBean
private UserService userService;
Summary
| Category | Annotations |
|---|---|
| Core | @SpringBootApplication |
| Web | @RestController, @GetMapping |
| DI | @Autowired, @Qualifier |
| JPA | @Entity, @Id |
| Transactions | @Transactional |