CDI Integration
JPAstreamer comes with first-party support for projects built on top of CDI. Dependency Injection is a fundamental feature of the CDI API. With this in mind, a JPAstreamer CDI producer can be used to quickly and easily integrate JPAstreamer into your CDI projects.
Installing the CDI producer
The installation process will differ depending on the build tool you’re using. In this guide, Maven and Gradle are covered.
Maven
For Maven projects, the following JPAstreamer dependency must be added to the project’s pom.xml file:
<dependencies>
<dependency>
<groupId>com.speedment.jpastreamer.integration.cdi</groupId>
<artifactId>cdi-jpastreamer</artifactId>
<version>${jpa-streamer-version}</version>
</dependency>
</dependencies>
Accessing JPAstreamer
Once the required dependencies are installed, you can access JPAStreamer using CDI’s Dependency Injection system:
@ApplicationScoped
public class DummyService {
@Inject
JPAStreamer jpaStreamer;
}
Direct field injection is usually not recommended, it’s preferable to inject your dependencies via a constructor:
@ApplicationScoped
public class DummyService {
private final JPAStreamer jpaStreamer;
@Inject
public DummyService(final JPAStreamer jpaStreamer) {
this.jpaStreamer = jpaStreamer;
}
}
JPAstreamer’s CDI injection relies on a EntityManagerFactory bean being present. If you’re using JPA, this is already handled for you.
|