✅ Core Annotations
Annotation | Description | Example |
@SpringBootApplication | Combines @Configuration, @EnableAutoConfiguration, @ComponentScan | @SpringBootApplication public class App {} |
@EnableAutoConfiguration | Enables Spring Boot’s auto-configuration mechanism | @EnableAutoConfiguration public class App {} |
@ComponentScan | Automatically scans and registers beans in the package | @ComponentScan("com.example") |
✅ Component Stereotypes
Annotation | Description | Example |
@Component | Generic stereotype for any Spring-managed bean | @Component public class MyBean {} |
@Service | Specialization of @Component for service layer | @Service public class MyService {} |
@Repository | Marks DAO classes and enables exception translation | @Repository public interface UserRepo extends JpaRepository {} |
@Controller | Marks MVC controller returning views | @Controller public class ViewController {} |
@RestController | Combines @Controller and @ResponseBody for REST | @RestController public class ApiController {} |
✅ Dependency Injection
Annotation | Description | Example |
@Autowired | Injects a Spring bean automatically | @Autowired private MyService service; |
@Qualifier | Specifies the bean to inject when multiple beans exist | @Qualifier("emailService") |
@Value | Injects values from properties file | @Value("${app.name}") private String name; |
✅ Configuration & Beans
Annotation | Description | Example |
@Configuration | Marks class as a source of bean definitions | @Configuration public class AppConfig {} |
@Bean | Declares a bean inside a @Configuration class | @Bean public MyService myService() {} |
@PropertySource | Load external property files | @PropertySource("classpath:app.properties") |
@EnableConfigurationProperties | Enables binding of external properties to config beans | @EnableConfigurationProperties(Config.class) |
✅ Web Annotations
Annotation | Description | Example |
@RequestMapping | Maps HTTP requests to handler methods | @RequestMapping("/users") |
@GetMapping | Shortcut for @RequestMapping(method = GET) | @GetMapping("/{id}") |
@PostMapping | Maps HTTP POST requests | @PostMapping("/") |
@PutMapping | Maps HTTP PUT requests | @PutMapping("/{id}") |
@DeleteMapping | Maps HTTP DELETE requests | @DeleteMapping("/{id}") |
@PathVariable | Binds URI path variables | @GetMapping("/{id}") public User get(@PathVariable Long id) |
@RequestParam | Extracts query parameters | @GetMapping public String find(@RequestParam String name) |
@RequestBody | Binds request JSON to Java object | @PostMapping public void add(@RequestBody User user) |
@ResponseBody | Returns method output as JSON | @ResponseBody public User get() |
@RequestHeader | Binds HTTP headers to method parameters | @GetMapping public String show(@RequestHeader("User-Agent") String userAgent) |
@ModelAttribute | Binds form data to a model class | @ModelAttribute User user |
@CrossOrigin | Enables CORS for REST endpoints | @CrossOrigin(origins = "*") |
✅ JPA & Persistence
Annotation | Description | Example |
@Entity | Marks a class as a JPA entity | @Entity public class User {} |
@Id | Marks the primary key | @Id private Long id; |
@GeneratedValue | Auto-generates primary key | @GeneratedValue(strategy = GenerationType.IDENTITY) |
@Column | Maps field to database column | @Column(name = "username") |
@OneToMany, etc. | Maps relationships | @OneToMany(mappedBy = "user") |
✅ Validation
Annotation | Description | Example |
@NotNull | Field must not be null | @NotNull private String name; |
@Size | String/collection must be within length range | @Size(min=2, max=30) |
Must be a valid email | @Email private String email; | |
@Valid | Triggers validation on method input | @PostMapping public void save(@Valid @RequestBody User u) |
✅ Testing
Annotation | Description | Example |
@SpringBootTest | Load full application context for testing | @SpringBootTest public class MyTests {} |
@WebMvcTest | Loads web layer only | @WebMvcTest(UserController.class) |
@DataJpaTest | Loads JPA components for testing | @DataJpaTest |
@MockBean | Adds mock bean to context | @MockBean private UserService service; |
✅ Transaction Management
Annotation | Description | Example |
@EnableTransactionManagement | Enables annotation-driven transaction management | @EnableTransactionManagement in main class |
@Transactional | Wraps method or class in a transaction | @Transactional public void save(User u) |
No comments:
Post a Comment