0

I'm developing Jersey-based RESTful Web Services. And, I'm choosing between DeltaSpike Data and Spring Data JPA for my repository layer. I have tried both of them. I'm amazed that they are almost the same.

DeltaSpike Data:

public interface AuthorRepository extends EntityRepository<Author, Long> {
}

Spring Data JPA:

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

But my problem is not as to which one is better and I should choose but, how to apply HK2 dependency injection.

By manually creating AuthorRepository and AuthorRepositoryImpl, I can simply do this configuration:

public class ApplicationBinder extends AbstractBinder {

    @Override
    protected void configure() {
        bind(AuthorRepositoryImpl.class).to(AuthorRepository.class).in(RequestScoped.class);
    }
}

But I could not figure out how to apply above similar configuration if I use either DeltaSpike Data or Spring Data JPA since there is not an implementation class for repository interface.

Any help will be appreciated. Thank you.

Julez
  • 1,010
  • 22
  • 44

1 Answers1

1

Personally I would go with Spring Data, as Jersey/HK2 already has an integration module for Spring. This will allow you to inject any Spring beans into your Jersey resources. And the Spring Data repository being a Spring bean, the injection works seamlessly; no need to configure anything with HK2/Jersey. All you would need to configure is the Data configuration for Spring. To get it working, you need to take the following steps:

1) Add the jersey-spring dependency.

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>${jersey2.version}</version>
</dependency>

This will give you the Spring/Jersey integration, allowing you to inject your Spring beans into your Jersey components. See also Combining Spring project and Jersey. It shows some different examples of using both Java configuration an XML configuration.

2) Configure the Spring/Data beans.

This will be your normal configuration, assuming you have done Data configuration with Spring before. This will consist of configuring the JPA vendor, transaction manager, and data source.

3) Inject your repository into your Jersey resource and enjoy.

You can find a complete example in this GitHub Repo

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720