Master Spring Boot Annotations: @SpringBootApplication, @RestController, @Autowired & More
This article explains the most commonly used Spring Boot annotations—including @SpringBootApplication, @RestController, @RequestMapping, @Autowired, @Value, @Component, @Service, @Repository, and @Configuration—providing clear descriptions and practical code examples for each.
Spring Boot provides many annotations that simplify configuration and development of Java applications. Below is a detailed overview of the most frequently used annotations.
@SpringBootApplication
This annotation marks the entry point of a Spring Boot application. It combines @Configuration, @EnableAutoConfiguration, and @ComponentScan, reducing boilerplate configuration.
@RestController
Indicates that a class is a RESTful controller, equivalent to @Controller combined with @ResponseBody.
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public List<User> getUsers() {
return userService.getUsers();
}
@PostMapping("/users")
public User addUser(@RequestBody User user) {
return userService.addUser(user);
}
}@RequestMapping
Used to map HTTP requests to handler methods or classes, specifying path, method, and parameters.
@RestController
@RequestMapping("/api")
public class UserController {
// ...
@GetMapping("/users/{id}")
public User getUserById(@PathVariable("id") Long id) {
return userService.getUserById(id);
}
}@Autowired
Enables automatic injection of beans by type, applicable to constructors, fields, or method parameters.
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
// ...
}@Value
Injects values from properties or YAML files, or literal constants, into fields.
@Component
public class AppConfig {
@Value("${app.name}")
private String appName;
@Value("${app.version}")
private String appVersion;
// ...
}@Component
Marks a class as a Spring component, allowing it to be detected and registered in the application context.
@Component
public class MyComponent {
// ...
}@Service
Designates a class as a service layer component, typically used together with DAO components.
@Service
public class UserServiceImpl implements UserService {
// ...
}@Repository
Indicates a class is a data access component, usually interfacing with the database.
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
// ...
}@Configuration
Marks a class as a source of bean definitions, replacing XML configuration files.
@Configuration
public class AppConfig {
// ...
}These annotations together form the core of Spring Boot development, enabling rapid setup and clean code organization.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
