I am using
Play-Framework 2.4 , Spring-Data-MongoDb
和Google Guice Dependency Injection
由 play-framework 提供,用于管理应用程序中的依赖项。但是当我使用Spring-Data
和Play
以及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]
}
.................................
我该如何解决这个问题?
答案 0 :(得分:0)
如果您使用Guice进行注射,则需要创建模块定义以将接口绑定到其实现。我没有在你的片段中看到你,那你做过吗?
请参阅https://www.playframework.com/documentation/2.4.x/ScalaDependencyInjection#Providing-custom-bindings