2015-07-11 68 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