Play Framework 2.4.1: How to migrate custom plugins - scala

As of Play 2.4 the Plugin class is deprecated and one should use the Module class instead.
I've understood file play.plugins is no longer necessary and custom modules should be registered in application.conf as documented here.
But how do I migrate my old plugins? The Module class doesn't contain methods onStart and onStop... Is there an example somewhere?

This pull request has the full Redis plugin migration from 2.3 to 2.4. They use the constructor for the onStart and ApplicationLifecycle for the onStop in SedisPoolProvider.
https://github.com/typesafehub/play-plugins/pull/148/files
Documentation explains that the goal is to provide bindings in a DI framework agnostic way. This is the reason I believe there is no trait with onStart and onStop to implement. The agnostic way is to use constructor and/or by injecting a lifecycle module like ApplicationLifecycle.

Related

RequestScoped in Play 2.7

I am trying to inject a UnitOfWork into my repositories using Guice in Play. I am trying to configure the UnitOfWork to be RequestScoped, but there does not seem to be a way to do this.
I have googled and been on stack overflow for a while now and have not come across a clear answer for this. What is the state of RequestScoped for Play in 2019?
Play Framework is Stateless by default. From its Web Page:
Play is based on a lightweight, stateless, web-friendly architecture.
So everything that you inject with Guice is created newly.
There is an exception if you annotate a class with Singleton then this class will be injected only once per Node.

Binding `Play.current.configuration` using dependency injection in Play 2.5?

I'm using Guice dependency injection in my Play 2.5 application, along with SecureSocial. SecureSocial's Registration controller calls a (deprecated) Play.current.configuration value eagerly, which so far as I can tell is incompatible with DI, and therefore I get a java.lang.RuntimeException: There is no started application error immediately at runtime. Is there a way I can ensure that Play.current.configuration is bound by Guice and thus available to the SecureSocial library? Or is my only option to re-write the Registration controller to call the configuration lazily?
SecureSocial is on the way to support Play 2.5
There is a 3.0-M6 version (12 days ago), did you try it?
https://groups.google.com/forum/#!topic/securesocial/YcrAay9eO2M
https://github.com/jaliss/securesocial

Dependency Injection in play/scala without using constructor injection

After migrating from PlayFramework version 2.4 to version 2.5, I need to inject dependencies.
I'm avoiding to use #Inject for constructor DI as in this the caller class of the class that has #Inject also needs to get the same dependencies injected to call the callee's constructor. This increases code complexity.
Please suggest any other way of DI which does not involve injecting to constructor.
MacWire looks like a great tool for constructor injection. It is macro-based and thus typesafe and non invasive. The README page has a very good documentation, definitely worth a look IMHO.

Does playframework have some sort of a library to work with guice?

In my play 2.1 app I was using Guice, but to bind my interfaces to implementations for IoC I was using this library:
https://github.com/sptz45/sse-guice
Is this still needed for play 2.4? If not, do they have their own helpers that are like sse-guice?
As the What’s new in Play 2.4 page states:
Play now supports dependency injection out of the box.
You have the following options for DI in Play 2.4.x:
An implementation that uses Guice out of the box
An abstraction that allows other JSR 330 implementations to be plugged in
All Play components can be instantiated using plain constructors or factory methods
Traits that instantiate Play components that can be mixed together in a cake pattern like style to assist with compile time dependency injection
You can read more about Play’s dependency injection support for Scala.

Scala and Annotation

Just thinking of implementing Guice in scala
Any sample code ?
Unless you specifically need Guice to add Scala to an existing Java-based project that uses framework, I'd advocate that you just use built-in language features instead.
Traits and implicits can give you all the Dependency Injection you'll need. You might also want to search online for the Cake pattern.
This post about DI in Scala will be able to help you to get going with Guice. See Using Google Guice topic.