Using Spring as a framework for dependency injection with 2.4.x?

I am learning playscala 2.4.2 and trying to get spring DI with it. I see that the game has a lot of 2.4.x changes, and the old way to override the GlobalSettings.getControllerInstance parameter seems to be no longer an option.

I came across this project https://github.com/jroper/play-spring , but it seems to be more of a POC, proving that spring DI is possible, but not as easy as in the earlier versions of the game. Will it be spring integration mechanism for current and future versions of the game, or can a simpler mechanism or structure be expected soon from the games community?

+7
java spring scala playframework
source share
3 answers

Please follow these steps:

Step1: Add the spring dependencies to the build.sbt file.

libraryDependencies += "org.springframework" % "spring-context" % "4.1.6.RELEASE" libraryDependencies += "org.springframework" % "spring-core" % "4.1.6.RELEASE" libraryDependencies += "org.springframework" % "spring-beans" % "4.1.6.RELEASE" libraryDependencies += "org.springframework" % "spring-aop" % "4.1.6.RELEASE" 

Step 2: Create a new class ( ApplicationGlobalSettings.java ) and implement the GlobalSettings class.

 package com.ranga.global.settings; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import play.Application; import play.GlobalSettings; public class ApplicationGlobalSettings extends GlobalSettings { private static final String APPLICATION_CONTEXT_XML = "applicationContext.xml"; private ConfigurableApplicationContext applicationContext; @Override public void beforeStart(Application application) { super.beforeStart(application); } @Override public void onStart(Application application) { super.onStart(application); applicationContext = new ClassPathXmlApplicationContext(APPLICATION_CONTEXT_XML); } @Override public void onStop(Application application) { super.onStop(application); if(applicationContext != null) { applicationContext.close(); } } 

}

Step 3: Create a new spring configuration file in the conf folder ( applicationContext.xml ). conf \ applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.ranga.services, com.ranga.daos"/> </beans> 

Step 4: Add the newly created GlobalSettings file location to the application configuration file ( conf / application.conf ).

 .....some more configuration here..... # Global Objects class application.global=com.ranga.global.settings.ApplicationGlobalSettings 

Step 5: Create a new service class in the com.ranga.service package (HelloWorldService.java).

 package com.ranga.services; import javax.inject.Inject; import org.springframework.stereotype.Service; import com.ranga.daos.HelloWorldDAO; @Service public class HelloWorldService { @Inject private HelloWorldDAO helloWorldDAO; public String sayHello() { return helloWorldDAO.sayHello(); } } 

Step6: Create a new dao class in the com.ranga.daos package ( HelloWorldDAO.java ).

 package com.ranga.daos; import org.springframework.stereotype.Repository; @Repository public class HelloWorldDAO { public String sayHello() { return "Hello Ranga!"; } } 

Step7: Finally, paste HelloWorldService into the Application.java file.

 package com.ranga.controllers; import javax.inject.Inject; import org.springframework.beans.factory.annotation.Autowired; import com.ranga.services.HelloWorldService; import play.*; import play.mvc.*; import views.html.*; public class Application extends Controller { @Inject private HelloWorldService helloWorldService; public Result index() { return ok(index.render(helloWorldService.sayHello())); } } 

Step 8: Finally, change the code for the index.scala.html file.

 @(message: String) <h1>@message</h1> 

Now let's do it ... launch the application.

+2
source share

Latest Play Version:

Create a Global class (Old global than extended GlobaSettings):

 @Singleton public class Global { private static final String APPLICATION_CONTEXT = "applicationContext.xml"; private ConfigurableApplicationContext applicationContext; @Inject public Global( ApplicationLifecycle lifecycle ) { applicationContext = new ClassPathXmlApplicationContext(APPLICATION_CONTEXT_XML); lifecycle.addStopHook( () -> { applicationContext.close(); return F.Promise.pure( null ); }); } } 

Create the ConfigurableApplicationContextModule class:

 public class ApplicationContextModule extends AbstractModule { @Override protected void configure() { bind( Global.class ).asEagerSingleton(); } } 

In application.conf add this:

 play.modules.enabled += "config.ApplicationContextModule" 

Create applicationContext.xml file:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="services, dao"/> </beans> 

After creating what was stated above, Ranga

+1
source share

Just in case, this may help someone, I also worked on a solution that works based on the jroper project: https://github.com/jroper/play-spring . The key was to use the spring scan function to β€œload” the play classes:

ctx.scan(packages:_*)

with default playpacks:

def defaultPackages(): Seq[String] = Seq("router", "play", "controllers")

The solution works with 1 hack: you need to add @ javax.inject.Named next to @Singleton annotations in the Play classes so spring can scan and load them (i.e. you need to β€œdevelop” the Play you use, but it rather small and easy change). So here is my sample application with SpringApplicationLoader: https://github.com/remithieblin/play24_spring

0
source share

All Articles