![]() |
VOOZH | about |
Annotations in Spring Boot are metadata that provide instructions to the Spring framework at runtime or compile time. Instead of using complex XML configurations, annotations allow developers to configure applications directly in Java code.
Example:
Indicates that a class provides configuration for a Spring Boot application. It is a specialized version of @Configuration and is automatically included in @SpringBootApplication.
@SpringBootConfiguration
public class AppConfig {
}
Enables Spring Boot’s auto-configuration mechanism, automatically configuring beans based on classpath dependencies and application properties.
@EnableAutoConfiguration
public class AppConfig {
}
Tells Spring where to search for components like @Controller, @Service, @Repository, etc.
@ComponentScan("com.example")
public class AppConfig {
}
Marks a class as a generic Spring-managed component. Spring automatically detects and registers it during component scanning.
@Component
public class EmailService {
}
Used in the service layer to define business logic and improve code readability.
@Service
public class UserService {
}
Used in the DAO layer to interact with the database. Automatically translates database-related exceptions into Spring exceptions.
@Repository
public class UserRepository {
}
Indicates that a class contains Spring configuration and bean definitions. Acts as a replacement for XML-based configuration.
@Configuration
public class AppConfig {
}
Used to define a Spring bean explicitly inside a configuration class. Gives full control over bean creation and lifecycle.
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Automatically injects required dependencies into a class. Eliminates the need for manual object creation.
@Autowired
private UserService userService;
Specifies which bean to inject when multiple beans of the same type exist.
@Autowired
@Qualifier("emailService")
private NotificationService service;
Marks a bean as the default choice among multiple candidates. Used when no @Qualifier is explicitly specified.
@Primary
@Component
public class SmsService implements NotificationService {
}
Used to create RESTful web services and automatically returns data in JSON or XML format.
@RestController
public class HelloController {
}
Maps HTTP requests to controller classes or methods. Defines the base URL path for request handling.
@RequestMapping("/api")
public class ApiController {
}
Annotation | HTTP Method | Explanation |
|---|---|---|
@GetMapping | GET | Retrieves data from the server |
@PostMapping | POST | Sends data to the server |
@PutMapping | PUT | Updates existing data |
@DeleteMapping | DELETE | Deletes data |
Example
@GetMapping("/users")
public List<User> getUsers() {
return userService.getAllUsers();
}
Extracts values from the URI path and binds them to method parameters.
@GetMapping("/users/{id}")
public User getUser(@PathVariable int id) {
return userService.getUser(id);
}
Reads query parameters from the request URL. Used for optional or filtering inputs.
@GetMapping("/search")
public String search(@RequestParam String keyword) {
return keyword;
}
Binds the HTTP request body to a Java object. Commonly used with POST and PUT requests.
@PostMapping("/users")
public User saveUser(@RequestBody User user) {
return userService.save(user);
}
Injects individual property values from application.properties or application.yml.
@Value("${server.port}")
private String port;
Binds a group of related configuration properties to a POJO in a type-safe manner.
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
}
Validation annotations ensure input data correctness.
Example
Handles specific exceptions within a controller. Allows custom error responses for exceptions.
@ExceptionHandler(Exception.class)
public String handleException() {
return "Error occurred";
}
Provides global exception handling across all controllers. Centralizes error-handling logic.
@ControllerAdvice
public class GlobalExceptionHandler {
}
Annotation | Purpose |
|---|---|
@Entity | Marks a class as a JPA entity |
@Table | Specifies table mapping |
@Id | Defines primary key |
@GeneratedValue | Auto-generates primary key values |
@Column | Maps class field to table column |