Here are some commonly used Spring Boot annotations with examples:
- @SpringBootApplication: This annotation is used to indicate the main class of a Spring Boot application. It combines three annotations:
@Configuration
,@EnableAutoConfiguration
, and@ComponentScan
.
1 2 3 4 5 6 |
@SpringBootApplication public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } |
- @RestController: This annotation is used to define a RESTful web service controller. It combines
@Controller
and@ResponseBody
annotations.
1 2 3 4 5 6 7 |
@RestController public class MyController { @GetMapping("/hello") public String hello() { return "Hello, World!"; } } |
- @RequestMapping: This annotation is used to map HTTP requests to a specific method or class. It supports various attributes like
value
,method
,params
, andheaders
.
1 2 3 4 5 6 7 8 |
@RestController @RequestMapping("/api") public class MyController { @GetMapping("/hello") public String hello() { return "Hello, World!"; } } |
- @Autowired: This annotation is used to inject dependencies automatically. It can be applied to fields, constructors, or setter methods.
1 2 3 4 5 6 7 8 9 |
@Service public class MyService { private MyRepository myRepository; @Autowired public MyService(MyRepository myRepository) { this.myRepository = myRepository; } } |
- @Component: This annotation is used to mark a class as a Spring component. It serves as a generic stereotype for any Spring-managed component.
1 2 3 4 |
@Component public class MyComponent { // Component implementation } |
- @Service: This annotation is used to mark a class as a service component. It is a specialization of
@Component
and is commonly used for the service layer in the application.
1 2 3 4 |
@Service public class MyService { // Service implementation } |
- @Repository: This annotation is used to mark a class as a data access component or repository. It is a specialization of
@Component
and is commonly used for database operations.
1 2 3 4 |
@Repository public class MyRepository { // Repository implementation } |
- @Configuration: This annotation is used to mark a class as a source of bean definitions. It is typically used with
@Bean
to define application configurations.
1 2 3 4 5 6 7 |
@Configuration public class MyConfiguration { @Bean public MyBean myBean() { return new MyBean(); } } |
Here are a few more commonly used Spring Boot annotations with examples:
- @GetMapping: This annotation is used to map HTTP GET requests to a specific method or class.
1 2 3 4 5 6 7 8 |
@RestController @RequestMapping("/api") public class MyController { @GetMapping("/hello") public String hello() { return "Hello, World!"; } } |
- @PostMapping: This annotation is used to map HTTP POST requests to a specific method or class.
1 2 3 4 5 6 7 8 |
@RestController @RequestMapping("/api") public class MyController { @PostMapping("/users") public User createUser(@RequestBody User user) { // Logic to create a new user } } |
- @PathVariable: This annotation is used to bind a method parameter to a URI template variable.
1 2 3 4 5 6 7 8 |
@RestController @RequestMapping("/api") public class MyController { @GetMapping("/users/{id}") public User getUserById(@PathVariable("id") Long id) { // Logic to fetch user by ID } } |
- @RequestBody: This annotation is used to bind the HTTP request body to a method parameter. It is commonly used in RESTful APIs to handle JSON or XML payloads.
1 2 3 4 5 6 7 8 |
@RestController @RequestMapping("/api") public class MyController { @PostMapping("/users") public User createUser(@RequestBody User user) { // Logic to create a new user } } |
- @ConfigurationProperties: This annotation is used to bind external configuration properties to a Java object. It allows easy retrieval of properties from configuration files or environment variables.
1 2 3 4 5 6 7 8 |
@Configuration @ConfigurationProperties(prefix = "app") public class AppConfig { private String name; private String version; // Getters and Setters } |
- @Value: This annotation is used to inject values from property files or environment variables into fields, constructors, or setter methods.
1 2 3 4 5 6 7 |
@Component public class MyComponent { @Value("${app.url}") private String apiUrl; // Component implementation } |
- @ConditionalOnProperty: This annotation is used to conditionally enable or disable beans based on the presence or value of a specific configuration property.
1 2 3 4 5 6 7 8 |
@Configuration public class MyConfiguration { @Bean @ConditionalOnProperty(name = "app.feature.enabled", havingValue = "true") public FeatureService featureService() { return new FeatureServiceImpl(); } } |
These are just a few more examples of commonly used annotations in Spring Boot. Spring Boot offers a rich set of annotations that help simplify the development of various aspects of your application, such as RESTful APIs, data access, configuration, and more.