Spring Boot not autowiring @Repository

2016-06-04 spring spring-boot spring-data spring-data-jpa

I'm trying to configure a datasource with Spring Boot but I'm getting org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mycompany.myapp.repository.UserRepository com.mycompany.myapp.service.AuthenticationServiceImpl.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mycompany.myapp.repository.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

These is my project structure:

Main Class:

package com.mycompany.myapp;
@ComponentScan(basePackageClasses = {com.mycompany.myapp.domain.user.User.class,
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

Domain Class:

package com.mycompany.myapp.domain.user
public class User {

    private long id;
    @Column(nullable = false)
    private String name;
    @Column(nullable = false)
    private String lastName;
    @Column(nullable = false)
    private String password;
    @Column(nullable = false)
    private String email;

    public User() {}

    public User(String email, String password){
        this.email = email;
        this.password = password;


package com.mycompany.myapp.repository;
public interface UserRepository extends CrudRepository<User, Long> {

    List<User> findByLastName(String lastName);


package com.mycompany.myapp.service;
public class AuthenticationServiceImpl implements AuthenticationService {

    private UserRepository userRepository;

    public User add(){
        User user = new User();
        user.setEmail("[email protected]");
        return user;



I guess my app is not detecting my @Repository annotation on UserRepository. As far as I know, Spring Boot will automatically set my class as @Repository because it is extending CrudRepository. What am I doing wrong? (Notice that my Application class is on top of packages hierarchy).


The standard way to configure is to create a base marker repository interface and provide that in the component scan.

public interface RepositoryPackage {

Keep the interface RepositoryPackage in the same package as UserRepository.

@ComponentScan(basePackageClasses= "RepositoryPackage.class")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

Have you tried @EnableJpaRepositories in your configuration ?

In main class of spring boot you have to use this below annotation :

@ComponentScan(basePackages = "basepackage")
@EntityScan(basePackages ="basepackage")
@EnableJpaRepositories(basePackages = "basepackage")

In Repository layer use below annotation :

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;


If you have any service layer then use below annotation in the implementation class:

import org.springframework.stereotype.Service;