Play-Framework 2.4: Verwenden Sie Spring Depedency Injection mit Play-Framework anstelle von Guice
Ich benutzeSpring-Depedency
Injektion stattPlay-Framework Guice
Abhängigkeitsinjektion, aufgrund unserer Anforderung müssen wir die meisten @ verwendSpring-Modules
in unserer Anwendung wieSpring-Data-Mongodb
etc. Aber das Problem ist, dass unsere Abhängigkeiten nicht richtig in den Controller eingebunden sind, wie unten dargestellt:
Meine Konfiguration:
@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");
}}
<p>My <code>built.sbt</code> dependencies:</p><pre><code>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
</code></pre><p>In <code>built.sbt</code>, i am commenting <code>routesGenerator := InjectedRoutesGenerator</code> for stop <code>Play Guice Dependency Injection</code>.</p><p>My route file:</p><pre><code># Home page
GET / @controllers.Application.index()
</code></pre><p>When i run the application, i am getting following error:</p><pre><code>- 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) [/] ->
</code></pre><p><a href="https://stackoverflow.com/imgs/Jrbfw.png" rel="nofollow noreferrer"><img src="/imgs/Jrbfw.png" alt="play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[NullPointerException: null]]"></a></p>
According to this error, Spring <code>@Autowire</code> annotation does not work properly. But i am not getting the reason, when spring <code>@Autowire</code> not worked? How could i resolve this issue?