replacing Global.getControllerInstance in Play 2.4

363 views
Skip to first unread message

Olja

unread,
Sep 25, 2015, 8:08:26 PM9/25/15
to play-framework
I have the following code in the 2.3.7 version of play of our app:

@Override
public <A> A getControllerInstance(Class<A> controllerClass) throws Exception {
return ctx.getBean(controllerClass);
}

ctx is Spring  application context. 

What is the best way to reimplement in 2.4? The migration documentation is unclear to me and refers to Guice. Switching from Spring to Guice is not feasible for our project at this time.  Thanks in advance for your help.

Thanks,
Olja

David Rollins

unread,
May 25, 2016, 5:04:33 PM5/25/16
to play-framework
Did you ever find a solution?

I'm working on the same thing now.

Greg Methvin

unread,
May 25, 2016, 8:13:33 PM5/25/16
to play-framework
Are you talking specifically about Spring integration? There was a recent post about it: https://groups.google.com/d/msg/play-framework-dev/wi9b5xqP0b8/hhi2z61iAQAJ It may be worth trying that approach. You may have to adapt it slightly to 2.4 but most of it should be the same.

Another thing you may consider is defining a custom application loader and just constructing the router from Spring (Something like https://www.playframework.com/documentation/2.5.x/JavaRoutingDsl#Providing-a-DI-router, except you get your instance from Spring instead of the routing DSL). The generated router code from the routes compiler (router.Routes) would only require bindings for the controllers. That doesn't give you "full" integration (Play internal classes aren't bound in Spring), but gives you the same level of functionality as with getControllerInstance.

--
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-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/d992c4ea-e148-49fc-88ca-f803614d8428%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Senior Software Engineer

Olja

unread,
May 26, 2016, 4:39:14 PM5/26/16
to play-framework
I recall using a Guice module facade as described in : http://stackoverflow.com/questions/31480232/what-is-an-equivalent-to-global-getcontrollerinstance-in-play-2-4. Ultimately we shelved the upgrade since we didn't want to use both Guice and Spring and didn't have the bandwidth to take on implementing a Spring only DI solution with 2.4 ourselves. 

Greg Methvin

unread,
May 26, 2016, 7:30:25 PM5/26/16
to play-framework
Hi Olja,

Just so you know, the solutions I suggested don't actually require Guice to work at all. When we separate out Guice support from core in 2.6, you'll be be able to remove the Guice dependency altogether.

I would like to see a good Spring module for Play since I think it will help the community, but right now the Play core team doesn't really have the bandwidth or the familiarity with Spring to take it on either. We would be happy to help if someone else wants to do it though.

Cheers,
Greg

--
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-framewor...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

David Rollins

unread,
May 27, 2016, 9:30:25 AM5/27/16
to play-framework
We have a long series of bindings in a ScalaModule that is created as part of the GlobalSettings.

i.e.


bind[ActiveDirectoryService].to[DefaultActiveDirectoryService].in[Singleton]
bind[UserProfileRepository].to[HazelcastUserProfileRepository].in[Singleton]
bind[DashboardConfigRepository].to[HazelcastDashboardConfigRepository].in[Singleton]
bind[UserProfileService].to[DefaultUserProfileService].in[Singleton]
bind[DashboardConfigService].to[DefaultDashboardConfigService].in[Singleton]

I had been moving more of that to use @ImplementedBy

I've tried adding a ApplicationLoader with a modules, but the bindings just don't seem to work the same way.  Or at least they don't get picked up.

I'm back to trying to figure out what I can do as a short term step without completely changing everything right now.

Greg Methvin

unread,
May 27, 2016, 3:08:51 PM5/27/16
to play-framework
So you have an existing Guice-based project you're trying to migrate? It should be enough to add your module to play.modules.enabled:

play.modules.enabled = "com.example.FooModule"

If you extend GuiceApplicationLoader you can also add your modules using the .load(yourModule) in the builder.

We could be of more help if you showed us the code you're using.

--
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-framewor...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

david....@gmail.com

unread,
May 30, 2016, 11:29:37 PM5/30/16
to play-fr...@googlegroups.com
One thing that I'm struggling with is that I thought Play 2.4 would still support GlobalSetting while encouraging everyone to move to modular design.

I'm discovered that the onStart method here doesn't fire


object Global extends WithFilters(new GzipFilter(), CSRFFilter(), AuditLoggingFilter) with GlobalSettings {

lazy val logger = Logger(this.getClass)
val shutdownHook = ShutdownHook()
override def onStart(app: Application): Unit = {
if (!Play.isTest) {
logger.info(s"Using Config: ${sys.props.get("config.resource").orElse(sys.props.get("config.file")).orElse(sys.props.get("config.url")).getOrElse("default")}")
}



application.global="settings.Global"


i'm now workign through issues with this code:

class AppModule(
environment: Environment,
config: Configuration
) extends AbstractModule with ScalaModule with AkkaGuiceSupport {

val playConfig = new PlayConfiguration(config)

override def configure(): Unit = {
//bind[Configuration].toInstance(config)
bind[SessionlessSecurityComponent].in[Singleton]


and slowly fixing issues that crop up.   

One key problem for me was changing;


     
      val queueActorRef = Akka.system.actorOf(InmateWebActivityQueuePusher.props(config, hz), "web-activity-queue-pusher")
      val activityFeedActor = Akka.system.actorOf(WebActivityImporter.props(queueActorRef), "iweb-activity-importer")

      bind[ActorRef].annotatedWith(Names.named("activityFeedActor")).toInstance(activityFeedActor)


To:


bind[ActorRef].annotatedWith(Names.named("activityFeedActor")).toProvider[ActivityFeedActorProvider]

Since the module no longer received a Play Application

--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/zLq7uCT6n08/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/CAA%3D11Hz7EJpC6Cmoi0a7B%3DbcQy5eLWZ8HUMyuRkJ6gjB8_6M6Q%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages