java - Play Framework 2.4 : Use Spring Depedency Injection with Play-Framework Instead of Guice

标签 java spring playframework dependency-injection spring-data-mongodb

我正在使用 Spring-Depedency 注入(inject)而不是 Play-Framework Guice 依赖注入(inject),因为我们的要求,我们需要使用大部分的 Spring-我们应用程序中的模块,如 Spring-Data-Mongodb 等。但问题是,我们的依赖项没有正确注入(inject) Controller ,如下所示:

我的配置:

@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 中,我正在评论 routesGenerator := InjectedRoutesGenerator 以停止 Play Guice Dependency Injection

我的路线文件:

# 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 注解无法正常工作。但我不明白原因,当 spring @Autowire 不起作用时?我该如何解决这个问题?

最佳答案

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

对于你的问题,经过我的研究,我发现了这些关于在 Play 2.4 中用 juice 替换/集成 Spring 的依赖注入(inject)的链接。但是,与 Spring for Play 2.4 的集成尚不存在,只有 James Ropper 完成的原型(prototype),如本期所述:

github.com/playframework/playframework/issues/4665

github.com/jroper/play-spring

github.com/spring-projects/spring-guice

关于java - Play Framework 2.4 : Use Spring Depedency Injection with Play-Framework Instead of Guice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32406005/

相关文章:

java - 如何在 Intellij IDEA 中将弹出窗口保持在一个屏幕上?

macos - Playframework:在 mac 中找不到激活器命令

java - ajax请求后渲染页面

java - Kotlin ViewPager2 : Class 'ScreenSlidePagerAdapter' is not abstract and does not implement base class member

java - 使用递归反转子数组的顺序

java - 为什么我不能将 T 类型的对象传递给 < 类型对象的方法?延伸 T>?

spring - WebSphereMBeanServerFactoryBean 的 NullPointerException

spring - 如何在 Spring-Data-Rest 中实现细粒度的访问控制?

java - Spring Boot : add new yml files to application config

java - Playframework DynamicForm 总是空值