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

All controllers and beans commands · Spring Boot 4 cheatsheet

Was this page helpful?