Dependency injection DeltaSpike数据或Spring数据JPA的HK2依赖注入

Dependency injection DeltaSpike数据或Spring数据JPA的HK2依赖注入,dependency-injection,jersey,spring-data-jpa,hk2,deltaspike,Dependency Injection,Jersey,Spring Data Jpa,Hk2,Deltaspike,我正在开发基于Jersey的RESTful Web服务。我正在为我的存储库层选择DeltaSpike数据和Spring数据JPA。我两个都试过了。我很惊讶他们几乎是一样的 DeltaSpike数据: public interface AuthorRepository extends EntityRepository<Author, Long> { } 但如果我使用DeltaSpike数据或Spring数据JPA,我无法理解如何应用上述类似配置,因为存储库接口没有实现类 任何帮助都将

我正在开发基于Jersey的RESTful Web服务。我正在为我的存储库层选择DeltaSpike数据和Spring数据JPA。我两个都试过了。我很惊讶他们几乎是一样的

DeltaSpike数据:

public interface AuthorRepository extends EntityRepository<Author, Long> {
}
但如果我使用DeltaSpike数据或Spring数据JPA,我无法理解如何应用上述类似配置,因为存储库接口没有实现类


任何帮助都将不胜感激。谢谢。

我个人会选择Spring数据,因为Jersey/HK2已经有了Spring的集成模块。这将允许您将任何SpringBean注入到您的Jersey资源中。Spring数据存储库是一个Springbean,注入可以无缝工作;无需使用HK2/Jersey配置任何内容。您需要配置的只是Spring的数据配置。要使其正常工作,您需要采取以下步骤:

1添加jersey spring依赖项。 这将为您提供Spring/Jersey集成,允许您将SpringBean注入Jersey组件。另见。它展示了使用Java配置和XML配置的一些不同示例

2配置Spring/数据bean。 这将是您的正常配置,假设您以前使用Spring进行过数据配置。这将包括配置JPA供应商、事务管理器和数据源

3将您的存储库注入到您的Jersey资源中并享受。 你可以在这篇文章中找到一个完整的例子

public interface AuthorRepository extends CrudRepository<Author, Long> {
}
public class ApplicationBinder extends AbstractBinder {

    @Override
    protected void configure() {
        bind(AuthorRepositoryImpl.class).to(AuthorRepository.class).in(RequestScoped.class);
    }
}
<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>${jersey2.version}</version>
</dependency>