我在用Spring-Depedency
注射代替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");
}}
My 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
In 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]]](https://i.stack.imgur.com/Jrbfw.png)
根据这个错误,Spring@Autowire
注释无法正常工作。但我不明白原因,当春天@Autowire
没工作?我该如何解决这个问题?