我正在使用
我的配置:
在
我的路由文件:
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) [/] ->
根据这个错误,Spring的@Autowire
注释没有正常工作。但我不知道原因是什么?在什么情况下Spring的@Autowire
无法正常工作?如何解决这个问题?