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:
My Configuration:
@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
dependencies:
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
, i am commenting routesGenerator := InjectedRoutesGenerator
for stop Play Guice Dependency Injection
.
My route file:
# 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) [/] ->
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?