Play-Framework 2.4:使用Spring依赖注入代替Guice与Play-Framework一起使用

3
我正在使用Spring-Dependency注入而非Play-Framework Guice依赖注入,因为我们的要求是需要在应用程序中使用大部分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) [/] ->

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

根据这个错误,Spring的@Autowire注释没有正常工作。但我不知道原因是什么?在什么情况下Spring的@Autowire无法正常工作?如何解决这个问题?


这是一个类似问题的答案:http://stackoverflow.com/a/43440349/3563224 - Mohit Sinha
3个回答

3
这个答案不能解决你的问题,但我希望它可以引导你用另一种方法来解决你的问题。首先,我使用集成在Play中的Spring,然而,Spring现在使用一个抽象类并处理自己的生命周期。Play(2.4.*)使用默认的Guice注入支持。

针对你的问题,经过我的研究,我发现了下面这些关于在Play 2.4中将Spring的依赖注入替换/集成为Juice的链接。然而,Play 2.4与Spring的集成还不存在,只有由James Ropper完成的原型,如下述问题所述:

github.com/playframework/playframework/issues/4665

github.com/jroper/play-spring

github.com/spring-projects/spring-guice


感谢@SerhatCan,我已经检查过spring-guice。但问题在于它像是Spring和Guice之间的桥梁。该项目的最后一次开发是1年前完成的。因此,在我们的实际项目中使用这些存在风险。但还是感谢你的努力。 - Harmeet Singh Taara

0

可能对于这个问题来说有点晚的回答。发表这篇文章是为了方便以后的参考。

我曾经面临过在Play 2.5.X中使用Spring Beans的类似挑战。Guice提供了一个名为guice-spring的API,用于将Spring与Guice注入器集成。使用此API,我能够配置Guice从我的Spring上下文中获取Bean以进行注入,特别是我的控制器Bean。请查看link以了解如何将Spring Bean绑定到Guice中。


0

为了将play与Spring应用程序集成,我提供了逐步开发的方法。请点击这里


在Play 2.4中,“Global”文件已被弃用。 - Harmeet Singh Taara
我使用过2.4.3版本,它是可用的。在这个版本中,他们删除了一个方法。只需尝试我的示例,它也可以在2.4上运行。 - Ranga Reddy
жҲ‘зҡ„е…іжіЁзӮ№жҳҜе…ідәҺеңЁPlayжЎҶжһ¶дёӯдҪҝз”ЁSpring-Dataе’ҢJava Basedй…ҚзҪ®гҖӮж №жҚ®дҪ зҡ„дҫӢеӯҗпјҢдҪ еҸӘжҳҜдҪҝз”ЁдәҶ@InjectжіЁи§ЈпјҢиҖҢжҲ‘дҪҝз”Ё@AutowiredжіЁи§Јж—¶еҮәзҺ°дәҶй”ҷиҜҜгҖӮ - Harmeet Singh Taara
是的,在 GlobalSetting 文件以外,我们不应该使用 Autowire 注解。除了 GlobalSettings 文件之外,我们需要使用 @Inject 注解。 - Ranga Reddy
我的应用程序模式和基于Java的配置存在主要问题。当我使用@Inject注解时,Google Guice会抛出一个关于找不到绑定的异常。 - Harmeet Singh Taara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接