简体   繁体   English

获取:通过字段“ userRepository”表达的不满意的依赖性

[英]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.

相关问题 创建名称为“ mainController”的bean时出错:通过字段“ userRepository”表示不满意的依赖性 - Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'userRepository' 通过“ productDao”字段表示的不满意依赖性 - Unsatisfied dependency expressed through field 'productDao' 通过字段“ clientRepository”表示不满意的依赖关系; - Unsatisfied dependency expressed through field 'clientRepository'; 通过字段“ sv”表示的不满意依赖性 - Unsatisfied dependency expressed through field 'sv' 通过字段“ freemarkerConfig”表示的不满意依赖性 - Unsatisfied dependency expressed through field 'freemarkerConfig' 通过“ employeeDao”字段表示不满意的依赖关系; - Unsatisfied dependency expressed through field 'employeeDao'; 通过字段和映射问题表达的不满意依赖性 - Unsatisfied dependency expressed through field and mapping issues 通过字段“springSecurityFilterChain”表达的不满意依赖 - Unsatisfied dependency expressed through field 'springSecurityFilterChain' 为什么会出现错误“创建名称为&#39;***&#39;的bean时出错:通过字段&#39;***&#39;表示的不满意依赖性” - Why Getting error “Error creating bean with name '***': Unsatisfied dependency expressed through field '***'” 通过字段表达的不满意的依赖(Spring Boot v 1.5.1) - Unsatisfied dependency expressed through field (Spring Boot v 1.5.1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM