编程语言
首页 > 编程语言> > java-在Jersey / hk2应用程序中配置EntityManager

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

作者:互联网

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

我正在使用这个问题(How to i properly configure an entitymanager in a jersey-hk2 application)作为指导

我花了一天的时间试图使其运行,但我有很多例外.我不知道错误在哪里.

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(Singleton):

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();
    }
}

}

MyApplication是:

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>

非常感谢

解决方法:

我认为您没有显示完整的stacktrace.测试时,我确实看到了帖子中显示的那部分跟踪,但这只是跟踪的一部分.主要原因是

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构造函数上所做的一样.

因此,请修复两件事:

>绑定持久单元名称

bind(persistenceUnitName).to(String.class).named(PERSISTENT_UNIT);

>通过添加@Inject批注创建注入点

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

标签:jersey-2-0,hk2,java
来源: https://codeday.me/bug/20191111/2019180.html