在Spring Boot单表中没有找到依赖类型的合格Bean [英] No qualifying bean of type found for dependency in Spring Boot single table
问题描述
我是Spring Boot的初学者。
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扩展了CrudRepository
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
List<User> findByLastName(String lastName);
List<User> findByAccNameAndPassword(String accName, String password);
}
用户 .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;
}
当我尝试运行该应用程序。
抛出异常:
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为
依赖项找到类型为[com.foo.dto.UserRepository]的
个合格Bean:预计至少有1个符合自动装配$ b要求的bean $ b此依赖项的候选。依赖注释:
{@ org.springframework.beans.factory.annotation.Autowired(required = true)
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)
推荐答案
我认为您需要在配置中启用它
I think you need to enable it in your configuration
@EnableJpaRepositories("com.foo.dto")
在您的@Configuration文件中。
in your @Configuration file.
这篇关于在Spring Boot单表中没有找到依赖类型的合格Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!