我正在探索play-scala 2.4.2并尝试让春季DI使用它。我发现在2.4.x中有很多变化,并且覆盖GlobalSettings.getControllerInstance的旧方法似乎不再是一个选项。
我遇到了这个项目https://github.com/jroper/play-spring,但它似乎更像是一个POC,证明Spring DI是可能的,但似乎并不像早期版本那样容易。这将是当前和未来游戏版本的弹簧集成机制,还是游戏社区很快就会有一个更简单的机制或框架?
答案 0 :(得分:2)
请按照以下步骤操作:
第1步:在 build.sbt 文件中添加spring依赖项。
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"
第2步:创建一个新类( 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.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>
步骤4:将新创建的GlobalSettings文件位置添加到应用程序配置文件( conf / application.conf )。
.....some more configuration here.....
# Global Objects class
application.global=com.ranga.global.settings.ApplicationGlobalSettings
Step5:在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();
}
}
第6步:在 com.ranga.daos 包( HelloWorldDAO.java )下创建一个新的dao类。
package com.ranga.daos;
import org.springframework.stereotype.Repository;
@Repository
public class HelloWorldDAO {
public String sayHello() {
return "Hello Ranga!";
}
}
Step7:最后在 Application.java 文件中注入 HelloWorldService 。
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()));
}
}
Step8:最后修改 index.scala.html 文件代码。
@(message: String)
<h1>@message</h1>
现在完成..运行应用程序。
答案 1 :(得分:1)
Play的最新版本:
创建Global类(旧全局而不是扩展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>
创建上述内容后
答案 2 :(得分:0)
万一它可以帮助某人,我还研究了一个基于jroper项目的解决方案:https://github.com/jroper/play-spring。 关键是使用spring的扫描功能来加载&#34; Play课程:
ctx.scan(packages:_*)
使用默认播放包:
def defaultPackages(): Seq[String] = Seq("router", "play", "controllers")
该解决方案适用于1 hack:你需要在Play类中的@Singleton注释旁边添加@javax.inject.Named,以便Spring可以扫描它们并加载它们(即你需要&#34; fork& #34;您正在使用的Play版本,但它相当小而且容易更改)。 所以这是我的SpringApplicationLoader示例应用程序: https://github.com/remithieblin/play24_spring