Guice play.exceptions.jpaeexception:JPA上下文未初始化
我有一个bean,它在构造函数中访问JPA,例如从数据库预加载缓存 申请启动时,我收到Guice play.exceptions.jpaeexception:JPA上下文未初始化,guice,playframework-1.x,Guice,Playframework 1.x,我有一个bean,它在构造函数中访问JPA,例如从数据库预加载缓存 申请启动时,我收到 Caused by: java.lang.IllegalStateException: Unable to create Guice injector 由于Guice注入,应用程序启动失败 1) Error injecting constructor, play.exceptions.JPAException: The JPA context is not initialized. JPA Entity
Caused by: java.lang.IllegalStateException: Unable to create Guice injector
由于Guice注入,应用程序启动失败
1) Error injecting constructor, play.exceptions.JPAException: The JPA context is not initialized.
JPA Entity Manager automatically start when one or more classes annotated with the @javax.persistence.Entity annotation are found in the application.
这个问题的根本原因是,Guice在开始使用inits JPA实体管理器之前创建了mybean的实例。其余的JPA代码工作正常,如果我在bean的构造函数中注释JPA调用,它也工作正常
要配置my Bean,请使用以下代码段:
public class MainGuiceModule extends AbstractModule {
@Override
protected void configure() {
bind(UserManager.class).to(UserManagerImpl.class).in(Singleton.class);
...
}
}
问题是,如何使用Play从构造函数访问JPA!还有Guice?最后我找到了避免这种异常的方法,我必须用
play.Play.plugin(JPAPlugin.class).startTx(true);
及
我会接受一个更优雅的答案
play.Play.plugin(JPAPlugin.class).closeTx(false);