No qualifying bean of type found for dependency in

2019-08-21 18:16发布

问题:

I am beginner with Spring Boot.

UserController.java

  @Controller
  @ComponentScan("com.foo.dto")
  public class UserController { 

  @Autowired
  UserRepository userRepository;

  @RequestMapping("/test")
  public void test() {
       System.out.println("PLEASE RUN");
  }

UserRepository extends CrudRepository

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

    List<User> findByLastName(String lastName);

    List<User> findByAccNameAndPassword(String accName, String password);
}

User.java

@Entity
    public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @NotNull
    private String firstName;
    @NotNull
    private String lastName;
    @NotNull
    private Date dob;
    @NotNull
    private String phone;
    @NotNull
    private String email;
    @NotNull
    private boolean isEmployer;
    @NotNull
    private String accountName;
    @NotNull
    private String password;

    protected User() {
    }

    public User(String firstName, String lastName, Date dob, String email, String phone, String accName,
            String password) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.dob = dob;
        this.email = email;
        this.phone = phone;
        this.accountName = accName;
        this.password = password;
        this.isEmployer = false;

    }

Is throwing exception when I try to RUN the application.

Exception thrown:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.foo.dto.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)

回答1:

I think you need to enable it in your configuration

@EnableJpaRepositories("com.foo.dto")

in your @Configuration file.