Java 在Play2.4.x中使用Spring作为依赖项注入框架?
我正在探索scala 2.4.2的游戏,并试图让spring DI使用它。我看到play 2.4.x中有很多变化,覆盖GlobalSettings.getControllerInstance的旧方法似乎不再是一个选项Java 在Play2.4.x中使用Spring作为依赖项注入框架?,java,spring,scala,playframework,Java,Spring,Scala,Playframework,我正在探索scala 2.4.2的游戏,并试图让spring DI使用它。我看到play 2.4.x中有很多变化,覆盖GlobalSettings.getControllerInstance的旧方法似乎不再是一个选项 我遇到过这个项目,但它似乎更像是一个POC,证明SpringDI是可能的,但似乎不像早期play版本那样容易。这将是当前和未来play版本的spring集成机制,还是play社区不久将推出更简单的机制或框架?请遵循以下步骤: 步骤1:在build.sbt文件中添加spring依赖项
我遇到过这个项目,但它似乎更像是一个POC,证明SpringDI是可能的,但似乎不像早期play版本那样容易。这将是当前和未来play版本的spring集成机制,还是play社区不久将推出更简单的机制或框架?请遵循以下步骤: 步骤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"
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()));
}
}
步骤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文件夹下创建一个新的spring配置文件(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>
步骤5:在com.ranga.service包(HelloWorldService.java)下创建一个新的服务类
步骤6:在com.ranga.daos包(helloworldao.java)下创建一个新的dao类
步骤7:最后在Application.java文件中注入HelloWorldService
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"
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()));
}
}
步骤8:最后修改index.scala.html文件代码
@(message: String)
<h1>@message</h1>
@(消息:字符串)
@信息
现在完成。。运行应用程序。最新版本的:
创建全局类(旧的全局设置,而不是扩展的全局设置):
创建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>
在创建了上面所述的内容(以防万一)之后,我还开发了一个基于jroper项目的解决方案:。 关键是使用spring的扫描功能“加载”游戏类:
ctx.scan(包:*)
使用默认播放的软件包:
def defaultPackages():Seq[String]=Seq(“路由器”、“播放”、“控制器”)
该解决方案使用1个hack:您需要在Play类中的@Singleton注释旁边添加@javax.inject.Named,以便Spring可以扫描并加载它们(即,您需要“分叉”您正在使用的Play版本,但这是一个非常小且容易的更改)。
下面是我使用SpringApplicationLoader的示例应用程序:
这里也提出了类似的问题:和。让我们期待一个解决方案。这个解决方案对我来说很有效:感谢链接,但我打算使用spring作为一个完整的DI框架,就像使用spring的所有接线语义和功能一样。我能在游戏2.3.9中做到这一点。另一种解决方案:,类似于。您可以使用@Inject在游戏控制器中连接SpringBean。是的,这不像以前那么容易。我已经回答了一个类似的问题
play.modules.enabled += "config.ApplicationContextModule"
<?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>