Play-Framework 2.4:无法使用google guice依赖注入映射Spring-Data-Mongodb存储库

时间:2015-09-02 19:43:33

标签: java spring playframework guice spring-data-mongodb

I am using Play-Framework 2.4 , Spring-Data-MongoDbGoogle Guice Dependency Injection play-framework 提供,用于管理应用程序中的依赖项。但是当我使用Spring-DataPlay以及Dependency Injection时,我的Spring-Data存储库不会映射到spring提供的内部实现。我的代码如下:

@Named @Singleton
public interface UserRepo extends GenericRepository<User, String>, UserRepoCustom{

}

@Named @Singleton
public class UserServiceImpl implements UserService{

@Inject
private UserRepo userRepo;

public void saveNewUser(User user) {
    userRepo.save(user);
}
..........................

在上面的情况下,异常是由playframework抛出的:

ProvisionException: Unable to provision, see the following errors:

1) No implementation for repository.UserRepo was bound.
  while locating repository.UserRepo
for field at    service.impl.UserServiceImpl.userRepo(UserServiceImpl.java:24)
  while locating service.UserService
for field at controllers.Application.userService(Application.java:15)
  while locating controllers.Application
for parameter 1 at router.Routes.<init>(Routes.scala:32)
 while locating router.Routes
 while locating play.api.inject.RoutesProvider
 while locating play.api.routing.Router

1 error

当我使用Spring注释而不是javax时,我得到了NullPointerException,如下所示:

@Repository
public interface UserRepo extends GenericRepository<User, String>, UserRepoCustom{

}

@Named @Singleton
public class UserServiceImpl implements UserService{

@Autowired
private UserRepo userRepo;

public void saveNewUser(User user) {
    userRepo.save(user);
}
.........................

以下是我的例外情节:play-Framework:

[NullPointerException: null]

private UserRepo userRepo;

public void saveNewUser(User user) {
    userRepo.save(user); //[NullPointerException: null]
}
.................................

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果您使用Guice进行注射,则需要创建模块定义以将接口绑定到其实现。我没有在你的片段中看到你,那你做过吗?

请参阅https://www.playframework.com/documentation/2.4.x/ScalaDependencyInjection#Providing-custom-bindings