[Java 2.5] How to use / inject JPAApi outside of controllers correctly?

374 views
Skip to first unread message

tho...@muellerleile.info

unread,
Sep 19, 2016, 1:51:57 AM9/19/16
to Play Framework
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()
}
}

Greg Methvin

unread,
Sep 19, 2016, 3:16:41 AM9/19/16
to play-framework
What do you mean by "test suites are not working"? Do you mean that you can't share the same application for all the tests in a single suite? That's because WithApplication starts a new app for each test.

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/e47b8f40-f36c-43cc-b4e1-f708aeb2a595%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Senior Software Engineer

Ala Schneider

unread,
Sep 20, 2016, 2:17:11 AM9/20/16
to Play Framework
It took me time to figure out, how to write working code. This post explains how to implement dependency injection in Play and shows code examples. This post explains, how to use Guice in tests. 

tho...@muellerleile.info

unread,
Sep 21, 2016, 3:22:36 AM9/21/16
to Play Framework
Hi Ala thanks for the links, i will  definitely check it out.

Thanks Greg,  for the hint regarding withApplication.

Benjamin Deslandes

unread,
Jul 6, 2018, 5:33:20 PM7/6/18
to Play Framework [deprecated]
Hello Ala,

Thanks for your answer which seems to be very helful for 2.5 projects. Since 2.6 makes play.Play deprecated, how can I inject Config or MessagesApi into a custom class, en enum etc.. I'm totally stucked about this..

Will Sargent

unread,
Jul 8, 2018, 1:03:13 PM7/8/18
to play-fr...@googlegroups.com
You would do something like:

import javax.inject.*;

public class MyComponent {
    private final MessagesApi messagesApi;

    @Inject
    public MyComponent(MessagesApi messagesApi) {
        this.messagesApi = messagesApi;
    }

    // ...
}



--
Please join our new forum at https://discuss.playframework.com!
The play-framework Google Group will soon be put into read-only mode.
For details, see https://blog.playframework.com/introducing-discuss-playframework-com/.
---
You received this message because you are subscribed to the Google Groups "Play Framework [deprecated]" group.

To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages