在 Jersey / hk2 应用程序中配置 EntityManager

Configure EntityManager in a Jersey / hk2 application

我正在尝试在我的 Jersey / Hk2 应用程序中注入 EntityManagerFactory(作为单例)和 EntityManager(在请求范围内)。

我正在使用这个问题 () 作为指导

我花了一天的时间试图得到它 运行 但我得到了很多例外。不知道哪里出错了

WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 3
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object 
available for injection at SystemInjecteeImpl(requiredType=EntityManagerFactory,parent=EntityManagerProvider...)
...
While attempting to resolve the dependencies of mypackages.EntityManagerProvider errors were found
...
java.lang.IllegalStateException: Unable to perform operation: resolve on mypackages.EntityManagerProvider
...
There was no object available for injection at SystemInjecteeImpl(requiredType=EntityManagerFactory,parent=EntityManagerProvider...
...
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of mypackages.EntityManagerProvider errors were found
...
java.lang.IllegalStateException: Unable to perform operation: resolve on mypackages.EntityManagerProvider

还有很多我删掉的例外

这是我的代码:

我的 EntityManagerFactoryProvider(单例):

public class EntityManagerFactoryProvider implements Factory<EntityManagerFactory> {

private static final String PERSISTENT_UNIT = "test";
private final EntityManagerFactory emf;

public EntityManagerFactoryProvider(@Named(PERSISTENT_UNIT) String persistentUnit) {
    emf = Persistence.createEntityManagerFactory(persistentUnit);
}
@Override
public EntityManagerFactory provide() {
    return emf;
}
@Override
public void dispose(EntityManagerFactory instance) {
    instance.close();   
}

} 

我的 EntityManagerProvider(每个请求)是:

public class EntityManagerProvider implements Factory<EntityManager> {

private final EntityManagerFactory emf;
private final CloseableService closeableService;

@Inject
public EntityManagerProvider(EntityManagerFactory emf, CloseableService closeableService) {
    this.emf = emf;
    this.closeableService = closeableService;   
}
@Override
public EntityManager provide() {
    final EntityManager em = emf.createEntityManager();
    this.closeableService.add(new Closeable() {
        @Override
        public void close() throws IOException {
            em.close();
        }
    });
    return em;
}
@Override
public void dispose(EntityManager entityManager) {
    if (entityManager.isOpen()) {
        entityManager.close();
    }
}

}

我的申请是:

public class MyApplication extends ResourceConfig {

public MyApplication() {
    register(new MyApplicationBinder());
    packages(true, "mypackages");
}

}

MyApplicationBinder 是:

 public class MyApplicationBinder extends AbstractBinder {

@Override
protected void configure() {
bindFactory(EntityManagerFactoryProvider.class)
   .to(EntityManagerFactory.class)
   .in(Singleton.class);
bindFactory(EntityManagerProvider.class)
   .to(EntityManager.class)
   .in(RequestScoped.class);
}
}

我的资源是:

@Path("myresource")
public class MyResource {

@Inject
EntityManager em;

/**
 * Method handling HTTP GET requests. The returned object will be sent
 * to the client as "text/plain" media type.
 *
 * @return String that will be returned as a text/plain response.
 */
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
    return "Got it!";
}

}

和Web.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!-- This web.xml file is not required when using Servlet 3.0 container,
 see implementation details 
 http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
    <servlet-name>MyApplication</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-
    class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>mypackage.MyApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>MyApplication</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>

非常感谢

我认为您没有显示完整的堆栈跟踪。测试时,我确实看到了您在 post 中显示的那部分轨迹,但这只是轨迹的一部分。主要原因是

java.lang.NoSuchMethodException: Could not find a suitable constructor in EntityManagerFactoryProvider

看看你的 EntityManagerFactoryProvider 构造器。

public EntityManagerFactoryProvider(@Named(PERSISTENT_UNIT) String persistentUnit) {
    emf = Persistence.createEntityManagerFactory(persistentUnit);
}

persistentUnit 应该来自哪里。你需要把它挂在活页夹里。而且 @Named 也没有定义注入点。它只是一个注入点的支持注解。它在 @Inject 时创建的注入点,就像在 EntityManagerProvider 构造函数上所做的那样。

所以修正两件事:

  1. 绑定持久化单元名称

    bind(persistenceUnitName).to(String.class).named(PERSISTENT_UNIT);
    
  2. 通过添加 @Inject 注释创建注入点

    @Inject
    public EntityManagerFactoryProvider(@Named(PERSISTENT_UNIT) String persistentUnit) {
        emf = Persistence.createEntityManagerFactory(persistentUnit);
    }