✅ Spring Boot Annotations
๐ 1. Main Application Setup
๐ฐ
@SpringBootApplication – This is a convenience annotation that combines:
@Configuration, @EnableAutoConfiguration, and @ComponentScan.
It is used on the main class to bootstrap and launch a Spring Boot application.
-
@Configuration– Indicates that the class contains Spring bean definitions. Beans defined here will be registered in the application context. -
@EnableAutoConfiguration– Tells Spring Boot to automatically configure beans based on the classpath, properties, and existing beans. Helps reduce manual configuration. -
@ComponentScan– Enables automatic scanning of packages for components like@Component,@Service,@Repository, and@Controller.
๐งฉ 2. Component & Bean Annotations
๐งฉ
@Component - Spring-managed component (auto-detected).๐ง
@Service - For business logic layer.๐️
@Repository - For data access layer with exception translation.๐ฎ
@Controller - Defines a web controller (MVC, returns views).๐
@RestController - Combines @Controller and @ResponseBody, returns JSON/XML.๐งช
@Bean - Manually declares a Spring bean inside @Configuration class.๐งท 3. Dependency Injection
๐
@Autowired - Automatically injects dependencies.๐ฏ
@Qualifier("beanName") - Specifies which bean to inject when multiple exist.๐
@Value("${property.name}") - Injects values from properties file.๐ 4. Web Request Mapping
๐
@RequestMapping("/path") - Generic HTTP mapping.๐ฅ
@GetMapping("/path") - Shortcut for GET requests.๐ค
@PostMapping, ๐@PutMapping, ๐️@DeleteMapping, ๐ฉน@PatchMapping - For respective HTTP methods.๐งญ
@PathVariable - Binds URL path variable (e.g., /user/{id}).๐
@RequestParam - Binds query parameters (e.g., ?name=John).๐ฆ
@RequestBody - Binds request body to Java object.๐ค
@ResponseBody - Returns object as JSON/XML in HTTP response.๐️ 5. JPA & Database
๐ท️
@Entity - Declares a JPA entity (maps to DB table).๐งพ
@Table(name = "users") - Specifies custom table name.๐
@Id - Defines primary key.๐ข
@GeneratedValue - Auto-generates primary key.๐
@Column(name = "email") - Maps field to DB column.๐ 6. Transactions
๐
@Transactional - Wraps method/class in a DB transaction.๐งช 7. Testing Annotations
๐งช
@SpringBootTest - Loads full app context for integration testing.๐งผ
@WebMvcTest - Loads only controller/web layer.๐
@DataJpaTest - For JPA repository testing with in-memory DB.๐งฑ
@MockBean - Adds a mock bean (overrides real one in tests).⚙️ 8. Profile & Configuration
⚙️
@Configuration - Defines Spring beans using @Bean methods.๐
@PropertySource("classpath:config.properties") - Loads custom property files.๐งช
@Profile("dev") - Activates bean/class for specific profile (e.g., dev).
Comments
Post a Comment