播放框架2.4:使用弹出式注入与游戏框架而不是Guice [英] Play-Framework 2.4: Use Spring Depedency Injection with Play-Framework Instead of Guice

查看:161
本文介绍了播放框架2.4:使用弹出式注入与游戏框架而不是Guice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring-Depedency 注入而不是 Play-Framework Guice 根据我们的要求,我们需要在我们的应用程序中使用大多数 Spring-Modules ,如 Spring-Data-Mongodb 等。但是问题是,我们的依赖关系在控制器中没有正确注入,如下所示:

I am using Spring-Depedency injection instead of Play-Framework Guice Depedency injection, because of our requirement, we need to use most of the Spring-Modules in our application like Spring-Data-Mongodb etc. But the problem is that, our dependencies are not inject properly in controller like as below:

我的配置:

@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");
}}

我的 build.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

build.sbt 中,我评论 routesGenerator:= InjectedRoutesGenerator 停止玩Guice依赖注入

我的路由文件:

# Home page
GET     /                           @controllers.Application.index()

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

When i run the application, i am getting following error:

- 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) [/] ->

根据这个错误,Spring @Autowire 注释无法正常工作。但是我没有得到理由,当春天 @Autowire 没有工作?我如何解决这个问题?

According to this error, Spring @Autowire annotation does not work properly. But i am not getting the reason, when spring @Autowire not worked? How could i resolve this issue?

推荐答案

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

This answer will not solve your problem but I hope it will guide you how to overcome your problem using a different approach. First of all, I'm using Spring integrated in Play however, Spring uses an abstraction class and handles its own life cycle for now. Play (2.4.*) uses its default Guice injection support.

对于您的问题,在我的研究之后,我发现了关于更换/集成Spring依赖注入与果汁的链接在播放2.4。然而,与Spring for Play 2.4的集成尚不存在,James Ropper只有这样一个原型:

For your problem, after my research I've found these links about replacing/integrating Spring's dependency injection with juice in Play 2.4. However, integration with Spring for Play 2.4 doesn't exist yet, there is only a prototype done by James Ropper as stated in this issue:

github.com/playframework/playframework/issues/4665

github.com/jroper/play-spring

github.com/spring-projects/spring-guice

这篇关于播放框架2.4:使用弹出式注入与游戏框架而不是Guice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆