我们如何解析@org.springframework.beans.factory.annotation.自动连线(必填项=真)[重复]

igetnqfo  于 2023-03-02  发布在  Spring
关注(0)|答案(1)|浏览(245)
    • 此问题在此处已有答案**:

Spring @Autowired can not wire Jpa repository(1个答案)
7天前关闭。
'我正在创建一个基本项目,如下所示...

@Entity
@Table(name="employee")
public class **Employee** {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private int id;
    
    @Column(name="name")
    private String name;
    
    @Column(name="email")
    private String email;
    
    @Column(name="bloodgroup")
    private String bloodGroup;
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public String getBloodGroup() {
        return bloodGroup;
    }
    
    public void setBloodGroup(String bloodGroup) {
        this.bloodGroup = bloodGroup;
    }

}
@RestController
public class **EmployeeController** {
`@Autowired     EmployeeService employeeService;`

    @GetMapping("/employee/{id}")
    public Employee getEmployeeDetails(@PathVariable("id")int id) {
        
        //db call
        Employee employee = employeeService.getEmployeeById(id);
        return employee;
                
    }
@Service
public class **EmployeeService** {

    `@Autowired
    private EmployeeRepo employeeRepo;`
    
    public Employee getEmployeeById(int id) {
        Employee emp = employeeRepo.findById(id).get();
        return emp;
        
    }

}
public interface **EmployeeRepo** extends JpaRepository\<Employee, Integer\>{

}

上面的类,我创建运行一个示例获取服务。当我运行该项目,我得到如下错误。

# Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeService': Error creating bean with name 'employeeService': Unsatisfied dependency expressed through field 'employeeRepo': No qualifying bean of type 'com.employeeservice.repo.EmployeeRepo' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

# Description:
# Field employeeRepo in com.employeeservice.service.EmployeeService required a bean of type 'com.employeeservice.repo.EmployeeRepo' that could not be found.
# The injection point has the following annotations:
# - @org.springframework.beans.factory.annotation.Autowired(required=true)

# Action:    
# Consider defining a bean of type 'com.employeeservice.repo.EmployeeRepo' in your configuration.

请在这个问题上帮助我。
谢谢

kg7wmglp

kg7wmglp1#

您会遇到这个问题,因为您没有使用@Repository注解标记Repository,并且由于这个原因Spring无法找到您的Repositionty来自动连接它。
为了解决这个问题-
@Repository
public interface **EmployeeRepo** extends JpaRepository\<Employee, Integer\>{
}
它会解决你的问题🙌

相关问题