No qualifying bean of type found for dependency in Spring Boot single table

半世苍凉 提交于 2019-12-11 16:28:01

问题


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.



来源:https://stackoverflow.com/questions/47336645/no-qualifying-bean-of-type-found-for-dependency-in-spring-boot-single-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!