Spring Boot Annotations
1. @SpringBootApplication
- This is the most commonly used annotation in Spring Boot. It is a combination of the following annotations:
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
It marks the main class of a Spring Boot application.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2. @RestController
- A convenience annotation that combines @Controller and @ResponseBody. It is typically used in RESTful web services.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
}
3. @RequestMapping
- This annotation is used to map web requests to specific handler methods or classes. It can map HTTP requests like GET, POST, PUT, DELETE, etc.
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@RequestMapping("/home")
public String home() {
return "Welcome to Home Page";
}
}
4. @GetMapping, @PostMapping, @PutMapping, @DeleteMapping
- These are specialized variants of @RequestMapping for common HTTP methods such as GET, POST, PUT, and DELETE.
import org.springframework.web.bind.annotation.*;
@RestController
public class CRUDController {
@GetMapping("/get")
public String getMethod() {
return "GET Method";
}
@PostMapping("/post")
public String postMethod() {
return "POST Method";
}
@PutMapping("/put")
public String putMethod() {
return "PUT Method";
}
@DeleteMapping("/delete")
public String deleteMethod() {
return "DELETE Method";
}
}
5. @Autowired
- This annotation is used for automatic dependency injection. It can be applied on constructors, methods, and fields.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User getUserById(Long id) {
return userRepository.findById(id).orElse(null);
}
}
6. @Component, @Service, @Repository
- These annotations are used to define Spring beans. They are automatically detected during classpath scanning and instantiated by Spring's container.
- @Component: Generic stereotype for any Spring-managed component.
- @Service: Specialization of @Component for service layer classes.
- @Repository: Specialization of @Component for DAO (Data Access Object) classes. It adds persistence exception translation.
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Repository;
@Component
public class GenericComponent {
// Generic component logic
}
@Service
public class UserService {
// Service layer logic
}
@Repository
public class UserRepository {
// Data Access logic
}
7. @Configuration
- Used to define configuration classes in Spring. It allows for the declaration of beans using the @Bean annotation.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
8. @Bean
- Marks a method as a bean provider method, meaning that it will return an object that Spring will register as a bean in the application context.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
9. @EnableAutoConfiguration
- This annotation enables Spring Boot's auto-configuration mechanism, which automatically configures the application based on the dependencies present in the classpath.
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@EnableAutoConfiguration
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
10. @ConditionalOnProperty
- Used to conditionally enable a bean based on a configuration property.
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConditionalConfig {
@Bean
@ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
public MyFeature myFeature() {
return new MyFeature();
}
}
11. @Transactional
- This annotation is used to define the scope of a database transaction. It can be applied to classes or methods.
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
@Service
public class TransactionalService {
@Transactional
public void performTransaction() {
// Transactional logic
}
}
12. @Value
- This annotation is used to inject property values from the application.properties or application.yml file.
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyComponent {
@Value("${app.name}")
private String appName;
public void printAppName() {
System.out.println(appName);
}
}
13. @PathVariable
- Used to extract values from URI templates.
import org.springframework.web.bind.annotation.*;
@RestController
public class PathVariableController {
@GetMapping("/user/{id}")
public String getUserById(@PathVariable("id") String id) {
return "User ID: " + id;
}
}
14. @RequestParam
- Used to extract query parameters from the URL.
import org.springframework.web.bind.annotation.*;
@RestController
public class RequestParamController {
@GetMapping("/search")
public String search(@RequestParam("q") String query) {
return "Search query: " + query;
}
}
List of other useful Annotations
- @Entity - @Table - @Id - @GeneratedValue - @Column - @Transient - @EntityListeners - @PrePersist - @PostPersist - @PreUpdate - @PostUpdate - @PreRemove - @PostRemove - @OneToOne - @OneToMany - @ManyToMany - @Query - @Param - @Transactional