2015-07-11 69 views
7

我正在探索play-scala 2.4.2並試圖讓春季DI與它一起工作。我發現在2.4.x中有很多變化,覆蓋GlobalSettings.getControllerInstance的舊方式似乎不再是一種選擇。使用Spring作爲2.4.x的依賴注入框架?

我遇到了這個項目https://github.com/jroper/play-spring,但它似乎更像是一個POC證明了Spring DI是可能的,但似乎不像早期的播放版本那麼容易。這是否會成爲當前和未來遊戲版本的彈簧整合機制,或者遊戲社區很快就會有一個更簡單的機制或框架?

+0

類似的問題也已經在這裏問:https://groups.google.com/forum/#!topic/play-framework/hFOtzSNSDsQ和https:/ /github.com/jroper/play-spring/issues/1。讓希望得到解決方案。 – Devabc

+0

這個爲我工作:https://github.com/zarinfam/play24-guice-spring – Devabc

+0

感謝您的鏈接,但我打算使用彈簧的總體DI框架與使用彈簧的所有接線語義和功能一樣。我可以在Play 2.3.9中做到這一點。 https://github.com/abbi-gaurav/play-spring-axon –

回答

2

請按照下列步驟操作:

第一步:添加春季依賴build.sbt文件。

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" 

第二步:創建一個新類(ApplicationGlobalSettings.java),並與GlobalSettings類實現。

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(); 
    } 
} 

}

步驟3:創建下CONF文件夾的新彈簧配置文件(的applicationContext.xmlCONF \ 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> 

第四步:將新創建的GlobalSettings文件位置添加到a應用程序配置文件(conf/application.conf)。

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

第五步:創建com.ranga.service包(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(); 
    } 
} 

第六步:com.ranga.daos創建一個新的DAO類包(HelloWorldDAO.java)。

package com.ranga.daos; 

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

第七步:最後注入的HelloWorldServiceApplication.java文件。

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())); 
    } 
} 

第八步:最後修改index.scala.html文件代碼。

@(message: String) 

<h1>@message</h1> 

現在完成..運行應用程序。

1

最新版本的Play

創建類全球(舊全球超過延長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); 
     }); 
    } 

} 

創建類ConfigurableApplicationContextModule:

public class ApplicationContextModule extends AbstractModule { 

    @Override 
    protected void configure() { 
     bind(Global.class).asEagerSingleton(); 
    } 

} 

在application.conf加這個:

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

創建文件的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="services, dao"/> 

</beans> 

上方產生什麼規定由Ranga

0

萬一它可以幫助別人後,我還曾對基於關閉jroper的項目工作的解決方案: https://github.com/jroper/play-spring。 的關鍵是使用Spring的掃描功能「負荷」播放類:

ctx.scan(packages:_*)

與默認該劇的包:

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

該解決方案可與1名黑客:你需要在Play類的@Singleton註釋旁邊添加@ javax.inject.Named,這樣Spring就可以掃描它們並加載它們(也就是說,您需要「分離」您正在使用的Play版本,但這是一個相當小而且容易更改)。 所以這是我與SpringApplicationLoader示例應用程序: https://github.com/remithieblin/play24_spring