Play-Framework 2.4:在 Play-Framework 中使用 Spring 依赖注入而不是 Guice [英] Play-Framework 2.4: Use Spring Depedency Injection with Play-Framework Instead of Guice
问题描述
我使用的是Spring-Depedency
注入而不是Play-Framework Guice
依赖注入,因为我们的需求,我们需要使用大部分Spring-我们应用程序中的模块
,例如 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");
}}
我的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
.
In built.sbt
, i am commenting routesGenerator := InjectedRoutesGenerator
for stop Play Guice Dependency Injection
.
我的路由文件:
# 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
注释不能正常工作.但是我没有得到原因,当 spring @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?
推荐答案
这个答案不能解决您的问题,但我希望它能指导您如何使用不同的方法解决您的问题.首先,我使用的是集成在 Play 中的 Spring,但是,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.
对于您的问题,经过我的研究,我发现了这些关于在 Play 2.4 中用果汁替换/集成 Spring 的依赖注入的链接.但是,与 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/spring-projects/spring-guice
这篇关于Play-Framework 2.4:在 Play-Framework 中使用 Spring 依赖注入而不是 Guice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!