编程语言
首页 > 编程语言> > java-Play-Framework 2.4:使用Spring Depedency Injection和Play-Framework代替Guice

java-Play-Framework 2.4:使用Spring Depedency Injection和Play-Framework代替Guice

作者:互联网

我使用Spring-Depedency注入而不是Play-Framework Guice Depedency注入,由于我们的要求,我们需要在应用程序中使用大多数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中,我正在注释routeGenerator:= InjectedRoutesGenerator以停止播放Guice依赖项注入.

我的路线文件:

# 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) [/] ->

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[NullPointerException: null]]

根据此错误,Spring @Autowire注释无法正常工作.但是我不明白为什么春天@Autowire不起作用?我该如何解决这个问题?

解决方法:

这个答案不会解决您的问题,但我希望它会指导您如何使用其他方法来解决您的问题.首先,我使用的是集成在Play中的Spring,但是Spring使用了一个抽象类并暂时处理了自己的生命周期. Play(2.4.*)使用其默认的Guice注入支持.

对于您的问题,在研究之后,我发现这些链接有关在Play 2.4中使用果汁替换/集成Spring的依赖项注入.但是,尚不存在与Spring for Play 2.4集成的问题,James Ropper仅完成了一个原型,如本期所述:

github.com/playframework/playframework/issues/4665

github.com/jroper/play-spring

github.com/spring-projects/spring-guice

标签:playframework,dependency-injection,spring-data-mongodb,spring,java
来源: https://codeday.me/bug/20191119/2038724.html