Guice AssistedInject找不到匹配的构造函数

时间:2018-06-05 08:30:39

标签: java guice assisted-inject

我刚开始学习Guice,但我已经遇到了问题。我有一个PlayerFactory接口,其中包含一个实现BlackjackPlayer

PlayerFactory.java

public interface PlayerFactory {
    Player createPlayer(String name);
    Player createPlayer(String name, boolean isDealer);
}

BlackjackPlayer.java

public class BlackjackPlayer implements PlayerFactory {
    private PointsCalculator pointsCalculator;

    public BlackjackPlayer(){
        pointsCalculator = new BlackjackPointsCalculator();
    }

    @Override
    public Player createPlayer(String name) {
        return new Player(pointsCalculator, name);
    }

    @Override
    public Player createPlayer(String name, boolean isDealer) {
        return new Player(pointsCalculator, name, isDealer);
    }
}

Player.class

public class Player{
    private PointsCalculator pointsCalculator;
    private List<Card> cardsInHand;
    private Integer points;
    private String name;
    private boolean isDealer;
    private boolean endedTurn;

    @AssistedInject
    public Player(PointsCalculator blackjackPointsCalculator, String name){
        pointsCalculator = blackjackPointsCalculator;
        cardsInHand = new ArrayList<>();
        points = 0;
        this.name = name;
        isDealer = false;
        endedTurn = false;
    }

    @AssistedInject
    public Player(PointsCalculator blackjackPointsCalculator, String name, boolean isDealer){
        pointsCalculator = blackjackPointsCalculator;
        cardsInHand = new ArrayList<>();
        points = 0;
        this.name = name;
        this.isDealer = isDealer;
        endedTurn = false;
    }

    public void addCardToHand(Card card) {
        cardsInHand.add(card);
        updatePoints();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Player)) return false;
        Player player = (Player) o;
        return getPoints() == player.getPoints() &&
            isDealer() == player.isDealer() &&
            isEndedTurn() == player.isEndedTurn() &&
            Objects.equals(pointsCalculator, player.pointsCalculator) &&
            Objects.equals(getCardsInHand(), player.getCardsInHand()) &&
            Objects.equals(getName(), player.getName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(pointsCalculator, getCardsInHand(), getPoints(), getName(), isDealer(), isEndedTurn());
    }

    public void updatePoints() {
        points = pointsCalculator.calculatePoints(cardsInHand);
    }

    public List<Card> getCardsInHand(){
        return cardsInHand;
    }

    public Integer getPoints(){
        return points;
    }

    public String getName(){
        return name;
    }

    public boolean isDealer() {
        return isDealer;
    }

    public boolean isEndedTurn() {
        return endedTurn;
    }

    public void setName(String name){
        this.name = name;
    }

    public void setDealer(boolean isDealer){
        this.isDealer = isDealer;
    }

    public void setEndedTurn(boolean endedTurn){
        this.endedTurn = endedTurn;
    }
}

我想使用Guice辅助注入来创建Player。以前我做过如下:

install(new FactoryModuleBuilder().build(PlayerFactory.class));

我知道这是错误的方式,因为我收到错误消息:

1) com.github.blackjack.model.Player has @AssistedInject constructors, but none of them match the parameters in method com.github.blackjack.factory.PlayerFactory.createPlayer().  Unable to create AssistedInject factory.
while locating com.github.blackjack.model.Player
at com.github.blackjack.factory.PlayerFactory.createPlayer(PlayerFactory.java:1)

2) com.github.blackjack.model.Player has @AssistedInject constructors, but none of them match the parameters in method com.github.blackjack.factory.PlayerFactory.createPlayer().  Unable to create AssistedInject factory.
while locating com.github.blackjack.model.Player
at com.github.blackjack.factory.PlayerFactory.createPlayer(PlayerFactory.java:1)

我尝试添加构造函数Player(String name)Player(String name, boolean isDealer)但它没有帮助。有人知道我该怎么做才能解决问题吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

您需要在注入参数上使用public interface PlayerFactory { Player createPlayer(String name); Player createPlayer(String name, boolean isDealer); } 注释:

<强> PlayerFactory.java

public class BlackjackPlayer implements Player {
  private final PointCalculator pointsCalculator;
  private final String name;
  private final boolean isDealer;
  @AssistedInject BlackjackPlayer(PointCalculator pointsCalculator, @Assisted String name) {
    this.pointsCalculator = pointsCalculator;
    this.name = name;
    this.isDealer = false;
  }
  @AssistedInject BlackjackPlayer(PointCalculator pointsCalculator, @Assisted String name, @Assisted boolean isDealer) {
    this.pointsCalculator = pointsCalculator;
    this.name = name;
    this.isDealer = isDealer;
  }
}

BlackjackPlayer.java (将其从工厂更改为实际播放器)

install(new FactoryModuleBuilder()
 .implement(Player.class, BlackjackPlayer.class)
 .build(PlayerFactory.class)
);

使用该模块如下:

public function up()
{
    Schema::create('courses_assign_students', function (Blueprint $table) {
       $table->increments('id');
       $table->integer('student_id')->unsigned();
       $table->foreign('student_id')->references('user_id')->on('additional_info_students');
       $table->integer('courses_id')->unsigned();
       $table->foreign('courses_id')->references('id')->on('courses');
       $table->timestamps();
     });     
}