首页 文章

在Spring Boot单表中没有找到依赖类型的限定bean

提问于
浏览
0

我是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;

    }

当我尝试 RUN 应用程序时抛出异常 .

Exception thrown:

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[com.foo.dto.UserRepository]的限定bean:期望至少有一个bean符合此依赖关系的autowire候选资格 . 依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)

1 回答

  • 0

    我认为您需要在配置中启用它

    @EnableJpaRepositories("com.foo.dto")
    

    在@Configuration文件中 .

相关问题