How it comes that CrudRepository is correctly autowired even without @Autowired annotation

Viewed 18

I would like to understand how auto wire works with CrudRepository: it doesn't matter if I will @Autowire instance of the interface that extends it - it will be mapped correctly regardless - so does anyone can explain to me what's going on under the hood?

Interface

public interface TeamRepository extends CrudRepository<Team, Long> {
    Team findByTeamName(String teamName);
}

Usage

@RestController
@RequestMapping("/teams")
public class TeamController {
    private final TeamRepository teamRepository;

    //@Autowired // Commented or not - it works
    public TeamController(TeamRepository teamRepository) {
        this.teamRepository = teamRepository;
    }

    @GetMapping("/{teamName}")
    public Team getTeamName(@PathVariable String teamName) {
        return teamRepository.findByTeamName(teamName);
    }
}
0 Answers
Related