我正在尝试从Play 2.3迁移到2.5但是我在更换GlobalSettings.OnStart方法时遇到了问题 . 在扩展GlobalSettings的旧Global类中,在onStartMethod中,我正在初始化Global Config并从DB中读取基本内容 . 我已经创建了一个新类,我将代码从onStart方法移动到文档中提到的这个构造函数 .

https://www.playframework.com/documentation/2.5.x/GlobalSettings https://www.playframework.com/documentation/2.4.x/PluginsToModules

我正在从AbstractModule进行eagerSingleton的绑定 . 该类正确启动但我一直收到错误:

GlobalBootstrapModule.configure:20 - 绑定OnStartConfig.:35 - Global ... on start引起:java.lang.RuntimeException:没有绑定到此线程的EntityManager . 尝试在JPAApi.withTransaction中包装此调用,或确保在此线程上设置HTTP上下文 .

这是我的代码:

要替换onStart的新类

public class OnStartConfig implements StartConfigInterface{

private final JPAApi jpaApi;

@Inject
public OnStartConfig(Application application, JPAApi jpa){
   this.jpaApi = jpa;
   Logger.debug("Global...on start");

   jpaApi.withTransaction( ()-> {
        GlobalConfiguration.aggregationCriteria = AggregationCriterion.getAll();
    });
}

我正在扩展的接口只是一个空的占位符 .

用于绑定的AbstractModule:

public class GlobalBootstrapModule extends AbstractModule {

@Override
protected void configure() {

Logger.debug("Binding");
bind(StartConfigInterface.class).to(OnStartConfig.class).asEagerSingleton();

    }
}

我在application.conf文件中启用了该模块:

play {
   modules {
   enabled += modules.GlobalBootstrapModule
   }
}

我想问题是由于缺少HttpContext . 我在哪里可以从初始化期间获取它?任何帮助将非常感激