Spring-Data-Mongodb depedencies not picked by Play

2019-02-27 12:18发布

问题:

I am using play-framework 2.4 with Spring-Data-Mongodb. But when i compile the project the compile throw an error:

[error] /home/james/play-spring-data-mongodb/app/configuration/SpringDataMongoConfiguration.java:10: package org.springframework.data.mongodb.config does not exist
[error] org.springframework.data.mongodb.config.AbstractMongoConfiguration
[error] /home/james/play-spring-data-mongodb/app/configuration/SpringDataMongoConfiguration.java:11: package org.springframework.data.mongodb.repository.config does not exist
[error] org.springframework.data.mongodb.repository.config.EnableMongoRepositories
[error] /home/james/play-spring-data-mongodb/app/configuration/SpringDataMongoConfiguration.java:25: cannot find symbol
[error]   symbol: class AbstractMongoConfiguration
[error] AbstractMongoConfiguration
[error] /home/james/play-spring-data-mongodb/app/configuration/SpringDataMongoConfiguration.java:24: cannot find symbol
[error]   symbol: class EnableMongoRepositories
[error] EnableMongoRepositories

The main thing is that, yesterday this works perfectly. But today i got this error, when trying to compile the project. My build.sbt file as below:

name := """play-mongo-template"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.6"

libraryDependencies ++= Seq(
    javaJdbc,
    cache,
    javaWs,
    "org.springframework" % "spring-context" % "4.1.6.RELEASE",
    "org.springframework.data" % "spring-data-mongodb-parent" % "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

Update

I found the problem, THe problem is that, our play-framework activator unable to download spring-framework dependency. When i open the .ivy folder and there is no jars are available. My system is connected with Internet, but still play sbt not resolve dependencies. How could i resolve this problem?

回答1:

To use the repository feature in Spring Data, the life cycle has to be managed by Spring.

Hence using it with Play 2.4 / 2.5, without adding Spring as a dependency is a problem because the default life cycle is managed by Guice.

I figured out a way to create the Spring beans and transfer its life cycle management to Guice. Hence they can be injected anywhere in the project by Guice instead of Spring.

For other components that don't have any Spring specific feature, they can be created normally and their life cycle will be managed by Guice.

For other components, which are Spring specific and can only run inside a Spring container, they can also be injected in the project and their life cycle will managed by Guice.

I modified the starter project included in Play 2.5 docs to demonstrate it.

Please have a look at this repository.
https://github.com/mohitsinha/play-java-spring-data-mongodb