Hi all,
i understand that DI offers many possibilities. However, as far as i understand out of the box injection of Play APIs such as JPAApi is only possible in controller classes.
However several use cases come to mind where using JPAApi might be beneficial, e.g. junit tests** (e.g for testing a model repository) or a
db authenticator in pac4j.
Posts on this mailling list suggest to use Play.current()... this is however deprecated.
So, what is the proper way to interact with a database (JPAApi) (or any other Play DI API) outside of controller classes?
I read the documentation and tried several types of custom bindings but without success.
Or do you generally discourage the use of JPAApi outside controller classes? If so why? What is the alternative?
Thanks
Thomas
** For using JPAapi in junit tests i do the following (i found it somewhere on the Internet). Unfortunately test suites are not working with this approach, thus the tests take a long time:
public abstract class AbstractTest extends WithApplication {
@Inject
protected JPAApi jpaApi;
@Inject
private Database database;
@Before
public void setupDatabaseAndHelper() {
Injector playInjector = app.injector();
com.google.inject.Injector guiceInjector = playInjector.instanceOf(com.google.inject.Injector.class);
guiceInjector.injectMembers(this);
}
@Override
protected Application provideApplication() {
return new GuiceApplicationBuilder()
.configure("DBSTUFF")
.build()
}
}