Play-Framework 2.4:使用Spring-Depedency Injection与Play-Framework而不是Guice

时间:2015-09-04 20:20:50

标签: java spring playframework dependency-injection spring-data-mongodb

我使用Spring-Depedency注入代替Play-Framework Guice Depedency注入,因为我们的要求,我们需要在我们的应用程序中使用Spring-Modules的大多数Spring-Data-Mongodb等。@Configuration @ComponentScan(basePackages={"service", "controllers"}) @EnableMongoRepositories(basePackages="repository") public class SpringDataMongoConfiguration extends AbstractMongoConfiguration{ private play.Configuration configuration = play.Configuration.root(); private MongoClientOptions mongoClientOptions(){ Builder builder = new Builder(); builder.connectionsPerHost(configuration.getInt("connections-per-host")); builder.connectTimeout(configuration.getInt("connections-timeout")); builder.maxConnectionIdleTime(configuration.getInt("max-connections-idle-time")); builder.maxConnectionLifeTime(configuration.getInt("max-connections-life-time")); builder.minConnectionsPerHost(configuration.getInt("max-connections-per-host")); builder.socketKeepAlive(configuration.getBoolean("socket-keep-live")); builder.socketTimeout(configuration.getInt("socket-timeout")); return builder.build(); } private ServerAddress serverAddress(){ ServerAddress serverAddress = new ServerAddress(new InetSocketAddress(configuration.getString("mongodb.uri"), configuration.getInt("mongodb.port"))); return serverAddress; } @Override protected String getDatabaseName() { return configuration.getString("mongodb.dbname"); } @Override public Mongo mongo() throws Exception { return new MongoClient(serverAddress(), mongoClientOptions()); } @Override protected String getMappingBasePackage() { return configuration.getString("package.scan"); }} 等。但问题是,我们的依赖关系没有在控制器中正确注入,如下所示:

我的配置:

built.sbt

我的libraryDependencies ++= Seq( javaJdbc, cache, javaWs, "org.springframework" % "spring-context" % "4.1.6.RELEASE", "org.springframework.data" % "spring-data-mongodb" % "1.7.2.RELEASE") // Play provides two styles of routers, one expects its actions to be injected, the // other, legacy style, accesses its actions statically. // routesGenerator := InjectedRoutesGenerator fork in run := true 个依赖关系:

built.sbt

routesGenerator := InjectedRoutesGenerator,我正在评论Play Guice Dependency Injection停止# Home page GET / @controllers.Application.index()

我的路线档案:

- play.api.libs.concurrent.ActorSystemProvider - Starting application    default Akka system: application
- play.api.Play - Application started (Dev)
********************************** userService : null
- application - 
! @6nbpln6jk - Internal server error, for (GET) [/] ->

当我运行应用程序时,我收到以下错误:

@Autowire

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[NullPointerException: null]]

根据此错误,Spring @Autowire注释无法正常工作。但是,当{{1}}春天不起作用时,我没有理由这样做?我怎么能解决这个问题?

3 个答案:

答案 0 :(得分:3)

这个答案不会解决您的问题,但我希望它能指导您如何使用不同的方法克服您的问题。首先,我在Play中使用Spring集成,但Spring现在使用抽象类并处理自己的生命周期。 Play(2.4。*)使用其默认的Guice注入支持。

对于你的问题,在我的研究之后,我发现了这些链接,用于在Play 2.4中替换/集成Spring的依赖注入和果汁。但是,与Spring for Play 2.4的集成尚不存在,James Ropper只做了一个原型,如本期所述:

github.com/playframework/playframework/issues/4665

github.com/jroper/play-spring

github.com/spring-projects/spring-guice

答案 1 :(得分:0)

为了整合Play应用程序我一步一步地给出了如何开发。点击here

答案 2 :(得分:0)

这个问题可能是一个迟到的答案。发布以便将来参考。

我遇到了在Play 2.5.X中使用spring bean的类似挑战。由guice提供的api guice-spring 将弹簧与guice注射器结合在一起。使用这个api我能够配置guice从我的spring上下文中获取bean以进行注入,尤其是我的控制器bean。检查这个link以了解如何将spring bean绑定到guice中。