【问题标题】:how to write bind method in play framework for 2 interfaces having same class implementations?如何在播放框架中为具有相同类实现的 2 个接口编写绑定方法?
【发布时间】:2016-06-15 05:46:02
【问题描述】:

这是我的第一个具有两个方法的接口服务类

package services;//this is my service interface class
import com.google.inject.ImplementedBy;
import dtos.MainDTO;
@ImplementedBy(UserServiceImpl.class)
public interface UserService {

    MainDTO getUserDetaile(Integer userId); 

    MainDTO getAllUserDetails();
}

这是我的第二个具有两个 DAO 查询方法的接口服务类

package services;//this is my DAO interface class
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

public interface UserRepository extends CrudRepository<Users, Long> {

    @Query("select userId,firstName from Users where userId =:userId")
    public List<Object[]> getUserDetails(@Param("userId") Integer userId);

    @Query("select userId,firstName from Users")
    public List<Object[]> getAllUserDetails();
}

这是上面两个接口的类实现类。 在第一个界面中,我使用 @Implementedby 注释进行了注释,并且工作正常。但是对于第二个界面需要添加什么?

package services;

import java.util.LinkedList;
import java.util.List;

import javax.inject.Inject;
import org.springframework.stereotype.Service;

import dtos.MainDTO;
import dtos.UserDTO;

@Service
public class UserServiceImpl implements UserService {

    private  UserRepository userRepository;

    @Inject
    public UserServiceImpl(UserRepository userRepository){
        this.userRepository = userRepository;
    }

    @Override
    public  MainDTO getUserDetaile(Integer userId){
        //method implementaion goes here
    }
    @Override
    public MainDTO getAllUserDetails() {

        //method implementaion goes here
}

面临这样的问题

【问题讨论】:

标签: playframework guice implementedby


【解决方案1】:

首先,您需要添加guice-repositoryhere it is

在您的模块中,您需要安装如下代码库

@Override
protected void configure() {
    //Repository classes auto-scanned by package name
   install(new ScanningJpaRepositoryModule(repositoriesBasePackageName, persistenceUnitName));
}

扫描完存储库后,您可以将接口直接注入您的服务或您想要的位置。

详情请见here

Play 模块的有效构造函数如下所示。

public YourModule(Environment environment, Configuration configuration) {
        this.environment = environment;
        this.configuration = configuration;
    }

【讨论】:

  • 首先谢谢你的朋友。我遵循你所说的。但现在我显示没有有效的构造函数模块 [Module] 无法实例化。
  • @HaiPandu 我已经用 Play 模块的有效构造函数更新了答案。
猜你喜欢
  • 1970-01-01
  • 2013-07-03
  • 2020-02-05
  • 1970-01-01
  • 2015-07-24
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 2015-03-11
相关资源
最近更新 更多