扩展CrudRepository的@Autowired接口如何工作?我想了解一些 [英] How does the @Autowired interface, which extends CrudRepository, works? I would like to have some insights

查看:188
本文介绍了扩展CrudRepository的@Autowired接口如何工作?我想了解一些的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的界面按如下方式扩展CrudRepository

Suppose my interface extends CrudRepository as follows

 @Repository
    public interface EmployeeRepository extends CrudRepository<Employee, Integer> 
    {
    }

并且我在Service类中使用EmployeeRepository接口,如下所示

and I use the EmployeeRepository interface in my Service class as follows

@Service
public class EmployeeService 
{

 @Autowired 
  EmployeeRepository employeeRepository;


 public List<Employee> getAllEmployee()
 {
     List<Employee> listEmp=new ArrayList<Employee>();
     employeeRepository.findAll().forEach(listEmp::add);
     return listEmp;


 }
}

和控制器如下

@RestController
public class WelcomeController 
{
    @Autowired
    EmployeeService empservice;

    @RequestMapping("/employees")
    public List<Employee> getEmployees()
    {
        return empservice.getAllEmployee();
    }

}

它给出了以下异常

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'welcomeController'的bean时出错:通过字段'empservice'表达的满意度不满意​​:创建名称为'employeeService'的bean错误:通过字段'employeeRepository表示的满意度不满足'

该错误很明显,因为任何类均未实现EmployeeRepository接口,并且

The error is obvious because interface EmployeeRepository is not implemented by any class and

@Autowired  
EmployeeRepository employeeRepository;

自动接线将失败,因为没有任何类在实现employeeRepository.

Autowired will fail because no class is implementing employeeRepository.

仍然,我对它的工作方式感到困惑,因为我在GitHub和教程上看到的每个代码都运行良好.

Still, I am confused as to how it works as, every code I saw on GitHub and tutorial, works perfectly.

我哪里出错了,即使没有类实现,@ Autowired在扩展CrudRepository的接口上如何工作;自动装配的基本规则是什么?也就是说,如果您要自动接线任何接口,则至少必须有一个类必须实现该接口,然后自动接线才能成功.

Where am I going wrong and how does @Autowired work on the interface which extends CrudRepository, even though no class is implementing it; which is the basic rule of Autowiring? That is, if you are auto wiring any interface, at least one class has to implement that interface then and then Autowiring will be successful.

推荐答案

嗯,关于Spring数据存储库,确实已经有了一个很好的答案,描述如下:

Well, there is indeed already a great answer about Spring Data Repositories described in : How are Spring Data repositories actually implemented? . However, when reading your question I believe there is a bit of confusion with regards the way the @Autowired works. Let me try to give the high level sequence of the events :

  1. 您将依赖项放在代码中的EmployeeRepository上:


@Autowired 
private EmployeeRepository employeeRepository;

  • 通过执行步骤(1),您向Spring容器指示在其启动过程期间,它应该找到实现EmployeeRepository的类的实例,并将其注入到目标容器中. @Autowired批注.我想在这里强调一个事实,为了使注入正常工作,您应该在运行时,而不是在编译过程中.

  • By doing step (1) you indicate to the Spring container that during its startup process it should find an instance of the class which implements EmployeeRepository and inject it into the target of the @Autowired annotation. I would like here to stress the fact that in order for the injection to work properly you should have the instance of the class implementing the required interface in the Spring container during the runtime and not during the compilation process.

    所以现在出现了一个逻辑问题:如果我们没有明确定义该类,那么实现UserRepository的类会在启动过程中出现在Spring容器中"?

    So now a logical questions comes in : "Where from a class implementing the UserRepository appears in the Spring container during its startup process if we have not explicitly defined that class" ?

    那是Oliver的详细回答:如何Spring Data存储库实际上已实现?.简而言之,发生的事情是在容器引导过程中,Spring Data会扫描所有存储库接口.创建实现这些接口的新类(代理);将这些类的实例放入Spring容器中,允许@Autowired查找并注入它们,就像对容器中的其他任何Spring bean一样.

    Thats were a detailed answer from Oliver comes in : How are Spring Data repositories actually implemented?. In the nutshell what happens is that Spring Data during the container bootstrap process scans for the all repositories interfaces; creates new classes (Proxies) which implement these interfaces; puts instances of those classes into the Spring container which allows for @Autowired to find and inject them as it would do for any other Spring bean within the container.

    同样,只有在您设置并正确配置了Spring Data的情况下,这些过程才有效,否则实际上注入将失败.

    And again, these process works only if you have Spring Data set up and correctly configured, otherwise indeed the injection would fail.

    希望这会有所帮助.

    这篇关于扩展CrudRepository的@Autowired接口如何工作?我想了解一些的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆