【问题标题】:How can we resolve @org.springframework.beans.factory.annotation.Autowired(required=true)我们如何解决@org.springframework.beans.factory.annotation.Autowired(required=true)
【发布时间】:2023-02-22 19:34:35
【问题描述】:

`我正在创建一个如下所示的基本项目......


@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\>{

}

我创建的上述类是为了运行一个示例 GET 服务。当我运行项目时,出现如下错误。

# 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.

请帮我解决这个问题..

谢谢

【问题讨论】:

  • 你有一个名为 EmployeeRepo 的类吗?它是否配置为 Spring 组件/服务/...?
  • 不,我们只有界面。
  • 那是你的问题,因为你的错误信息已经说明了
  • 我们不能在接口之上创建@Autowired 吗?

标签: java spring-boot


【解决方案1】:

你遇到这个问题是因为你没有用 @Repository 注释标记存储库,并且因为这个 Spring 无法找到你的 Reposiroty 来自动装配它。

要解决此问题-

@Repository
public interface **EmployeeRepo** extends JpaRepository&lt;Employee, Integer&gt;{
}

它会解决你的问题?

【讨论】:

  • 如果它解决了你的问题,请将其标记为已回答,否则请确保你正在对你的完整包执行@ComponentScan
  • 谢谢@Daksharaj,我试过了你说的。但是,我遇到了同样的问题。
猜你喜欢
  • 2017-02-27
  • 2020-05-16
  • 2014-04-28
  • 2021-04-06
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
相关资源
最近更新 更多