我已经设法在Jersey,HK2和一个简单的GrizzlyServer中设置我自己的服务类的注入(进入资源类).(基本上遵循这个例子.)
我现在好奇将JPA EntityManagers注入我的资源类最好的是什么?(我目前正在考虑将一个请求作为一个工作单元).我目前正在探索的一个选项是以Factory<EntityManager>下列方式使用a :
class MyEntityManagerFactory implements Factory<EntityManager> {
EntityManagerFactory emf;
public MyEntityManagerFactory() {
emf = Persistence.createEntityManagerFactory("manager1");
}
@Override
public void dispose(EntityManager em) {
em.close();
}
@Override
public EntityManager provide() {
return emf.createEntityManager();
}
}
Run Code Online (Sandbox Code Playgroud)
并按如下方式绑定它:
bindFactory(new MyEntityManagerFactory())
.to(EntityManager.class)
.in(RequestScoped.class);
Run Code Online (Sandbox Code Playgroud)
问题是dispose从不调用-method.
我的问题:
(我宁愿不依赖于重量级容器或额外的依赖注入库来覆盖这个用例.)
我有一个使用JPA持久性的jersey-2/hk2应用程序.将EntityManager在这样的约束启动
public MyApplication() {
// ...
register(new AbstractBinder() {
@Override
public void configure() {
bindFactory(EmFactory.class)
.to(EntityManager.class)
.in(RequestScoped.class);
}
});
}
Run Code Online (Sandbox Code Playgroud)
与工厂类一样
public class EmFactory implements Factory<EntityManager> {
private static final String PERSISTENCE_UNIT = "unit";
private EntityManagerFactory emf;
private CloseableService closeableService;
@Inject
public EmFactory(@Named(PERSISTENCE_UNIT) String persistenceUnit,
CloseableService closeableService) {
emf = Persistence.createEntityManagerFactory(persistenceUnit);
this.closeableService = closeableService;
}
@Override
public EntityManager provide() {
final EntityManager entityManager = emf.createEntityManager();
closeableService.add(new Closeable() {
@Override
public void close() throws IOException {
if(entityManager.isOpen()) {
entityManager.close(); …Run Code Online (Sandbox Code Playgroud) 对于我的生活,我无法通过hk2获得Jersey来自动发现@Service注释类并注入它们.我试图遵循堆栈溢出,泽西和hk2文档的每一个建议,仍然没有运气.我试图将一个简单的echo服务注入Jersey资源.骨架是从Jersey的简单webapp maven原型生成的,我试图扩展它.这是我到目前为止:
的pom.xml
<build>
<finalName>sandbox</finalName>
<plugins>
<plugin>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-inhabitant-generator</artifactId>
<version>2.3.0</version>
<executions>
<execution>
<configuration>
<verbose>true</verbose>
</configuration>
<goals>
<goal>generate-inhabitants</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
web.xml中
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>my.package.jerseytest</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>my.package.jerseytest.application.Application</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Run Code Online (Sandbox Code Playgroud)
my.package.jerseytest.application.Application
public class Application extends ResourceConfig {
public Application() {
ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
}
}
Run Code Online (Sandbox Code Playgroud)
my.package.jerseytest.service.EchoService
@Service …Run Code Online (Sandbox Code Playgroud)