在 Spring 单引导 table 中找不到依赖类型的合格 bean
No qualifying bean of type found for dependency in Spring Boot single table
我是 Spring 引导的初学者。
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: 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)
我认为您需要在配置中启用它
@EnableJpaRepositories("com.foo.dto")
在您的@Configuration 文件中。
我是 Spring 引导的初学者。
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: 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)
我认为您需要在配置中启用它
@EnableJpaRepositories("com.foo.dto")
在您的@Configuration 文件中。