Spring Boot Annotations You Must Know
✅ Spring Boot Annotations You Must Know
Spring Boot uses annotations extensively to reduce boilerplate code and simplify configuration. Whether you’re building REST APIs, connecting to a database, or writing business logic, knowing the right annotations is crucial.
π§ 1. Application-Level Annotation
@SpringBootApplication
Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Marks the main class of a Spring Boot application.
java
Copy
Edit
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
π¦ 2. Dependency Injection Annotations
@Component
Marks a class as a Spring-managed component.
@Service
Specialization of @Component for service-layer classes.
@Repository
Specialization of @Component for data-access layer.
Enables exception translation.
@Autowired
Injects dependencies automatically by type.
java
Copy
Edit
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
}
π 3. Web & REST Annotations
@RestController
Combines @Controller and @ResponseBody.
Returns JSON or XML responses by default.
@RequestMapping
Maps HTTP requests to handler methods or controllers.
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping
Shorthand for specific HTTP method mappings.
java
Copy
Edit
@RestController
@RequestMapping("/api/users")
public class UserController {
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userService.findUser(id);
}
}
π️ 4. Data and Persistence Annotations
@Entity
Marks a class as a JPA entity.
@Id
Marks a primary key field in an entity.
@GeneratedValue
Specifies how the primary key is generated.
@Table, @Column
Customize table and column mappings.
@Repository
Indicates a DAO (Data Access Object) component.
java
Copy
Edit
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
}
⚙️ 5. Configuration and Bean Management
@Configuration
Marks a class as a source of bean definitions.
@Bean
Declares a bean explicitly in a configuration class.
java
Copy
Edit
@Configuration
public class AppConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
π― 6. Testing Annotations
@SpringBootTest
Loads the full application context for integration tests.
@WebMvcTest
Tests only the web layer (controllers).
@DataJpaTest
Tests only JPA components (repositories).
@MockBean
Creates and injects a mock bean into the Spring context.
π§ͺ 7. Miscellaneous Annotations
@Value
Injects values from application.properties or application.yml.
java
Copy
Edit
@Value("${app.name}")
private String appName;
@EnableScheduling
Enables scheduled tasks using @Scheduled.
@Scheduled
Marks a method to be run periodically.
✅ Summary Table
Annotation Purpose
@SpringBootApplication Main entry point
@RestController Marks REST controller
@Service, @Repository, @Component Dependency injection
@Autowired Auto-inject beans
@Entity, @Id ORM mapping
@GetMapping, etc. Map HTTP methods
@Configuration, @Bean Java-based config
@SpringBootTest Integration testing
Learn Full Stack JAVA Course in Hyderabad
Read More
Dependency Injection in Spring Framework
Creating REST APIs using Spring Boot
Spring Boot vs Spring MVC – Key Differences
What is Spring Framework? An Overview
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment