Play-Framework 2.4: Use Spring Depedency Injection

2019-02-24 15:24发布

问题:

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?

回答1:

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.

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/jroper/play-spring

github.com/spring-projects/spring-guice



回答2:

To integrate play with Spring application i given step by step how to develop. Click here



回答3:

May be a late answer for this question. Posting it for the benefit of future reference.

I faced a similar challenge of using spring beans in Play 2.5.X. There is an api guice-spring provided by guice to integrate spring with guice injector. Using this api I was able to configure guice to fetch beans form my spring context for injection, especially my controller beans. Check this link to see how to bind a spring bean into guice.