Spring Boot Annotations Cheat Sheet — Ultimate Reference for Java Developers
Introduction
Spring Boot simplifies Java development by reducing boilerplate and providing powerful annotations. This cheat sheet is a quick reference to the most essential Spring Boot annotations that every developer should know — perfect for daily use and interview prep.
Core Spring Boot Annotations
@SpringBootApplication
Marks the main class of a Spring Boot application. Combines @Configuration
, @EnableAutoConfiguration
, and @ComponentScan
.
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
Component Scanning & Bean Management
@Component
, @Service
, @Repository
, @Controller
Used to mark classes for component scanning and dependency injection.
@Component // Generic Spring-managed bean
@Service // Service-layer component
@Repository // DAO component
@Controller // Web controller
@Autowired
Injects dependencies automatically.
@Autowired
private MyService myService;
@Bean
Manually define a bean in a @Configuration
class.
@Bean
public MyBean myBean() {
return new MyBean();
}
Web Development Annotations
@RestController
Marks a class as a RESTful controller. Combines @Controller
and @ResponseBody
.
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}
@RequestMapping
, @GetMapping
, @PostMapping
, etc.
Map HTTP requests to controller methods.
@GetMapping("/api/users")
public List<User> getAllUsers() { ... }
@PostMapping("/api/users")
public User createUser(@RequestBody User user) { ... }
@PathVariable
& @RequestParam
Extract values from URL path or query parameters.
@GetMapping("/user/{id}")
public User getUser(@PathVariable Long id) { ... }
@GetMapping("/search")
public String search(@RequestParam String q) { ... }
@RequestBody
& @ResponseBody
Used to bind or return JSON objects.
Configuration & Profiles
@Configuration
Marks a class that provides Spring bean definitions.
@Value
Injects values from application.properties
or application.yml
.
@Value("${app.name}")
private String appName;
@Profile
Activates beans only under specific Spring profiles.
@Profile("dev")
@Bean
public DataSource devDataSource() { ... }
Testing Annotations
@SpringBootTest
Runs a full Spring Boot application context for integration testing.
@WebMvcTest
, @DataJpaTest
Test specific layers (controllers, repositories) with minimal context.
📌 Bonus Tips
- Use
@RestController
instead of@Controller
+@ResponseBody
@RequestParam
is optional,@PathVariable
is required by default- Keep configuration values external for better flexibility
Conclusion
Spring Boot annotations make your code cleaner, more modular, and easier to manage. Save this cheat sheet and revisit whenever you need a quick reminder or want to onboard a new developer fast.