[英]Getting: Unsatisfied dependency expressed through field 'userRepository'
Unsatisfied dependency expressed through field 'userRepository' 通过字段“ userRepository”表示的不满意的依赖性
Can anyone tell me what I'm missing or need to add? 谁能告诉我我所缺少或需要添加的内容吗? Thanks a lot in advance! 在此先多谢!
Here's a picture of my project structure. 这是我的项目结构的图片。
User.java User.java
package org.codigo.entites;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
private String fname;
private String lname;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public User(String fname, String lname) {
this.fname = fname;
this.lname = lname;
}
public User() {
}
@Override
public String toString() {
return "User [id=" + id + ", fname=" + fname + ", lname=" + lname + "]";
}
}
UserRepository.java UserRepository.java
package org.codigo.repositories;
import org.codigo.entites.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
Your UserRepository
is interface, you cannot create bean for interface 您的UserRepository
是interface,您不能为该接口创建bean
public interface UserRepository extends JpaRepository<User, Long>
Declare UserRepository
as class with method implementations and annotate with @Repository
annotation, if JpaRepository
is interface then you should use implements
keyword 声明UserRepository
与方法的实现类,并标注@Repository
注解,如果JpaRepository
是接口,那么你应该使用implements
关键字
@Repository
public class UserRepository extends JpaRepository<User, Long>
Whenever Spring says Unsatisfied dependency
, it does not know how to obtain an instance of the desired type. 每当Spring说Unsatisfied dependency
,它都不知道如何获取所需类型的实例。
To let Spring know and even implement your repository (just the interface), you should 为了让Spring知道甚至实现您的存储库(只是接口),您应该
add @EnableJpaRepositories
to MyAppApplication
: 将@EnableJpaRepositories
添加到MyAppApplication
:
@SpringBootApplication
@EnableJpaRepositories(basePackage = "org.codigo.repositories")
public class MyAppApplication implements CommandLineRunner { ... }
This seems like magic at first - behind the scenes Spring will instantiate a dynamic proxy for your repository which intercepts all method calls and finds out what to do, based on the called method's name, its parameters and return type. 首先,这似乎很神奇-幕后,Spring将为您的存储库实例化一个动态代理,该代理将拦截所有方法调用,并根据被调用方法的名称,其参数和返回类型来确定要执行的操作。
Reference: 参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.