Spring Boot 4 · Controllers and beans
Declare a service
Register a class as a bean. Spring injects constructor arguments.
@Service
public class UserService {
private final UserRepository repository;
public UserService(UserRepository repository) {
this.repository = repository;
}
}
Official Spring Boot 4 documentation
More controllers and beans commands
- Write a REST controller
- Accept a JSON body
- Define a JPA entity
- Create a repository
- Handle exceptions globally
All controllers and beans commands · Spring Boot 4 cheatsheet