![](/img/trans.png)
[英]Spring-Boot create bean with out name will cause “NoSuchBeanDefinitionException, No qualifying bean of type[]found for dependency ”
[英]No qualifying bean of type found for dependency in Spring Boot single table
我是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個符合該依賴條件的自動裝配候選Bean。 依賴項注釋:{@ org.springframework.beans.factory.annotation.Autowired(required = true)
我認為您需要在配置中啟用它
@EnableJpaRepositories("com.foo.dto")
在您的@Configuration文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.