尽管有不同的构造函数@inject

发布于 2025-02-01 13:23:16 字数 3419 浏览 4 评论 0 原文

我正在关注Dagger ATM教程在线,并在页面末尾遇到了问题: https://dagger.dev/tutorial/03-first-command

我已将所有代码丢弃了以下所有代码以易于参考,但实际上我了解的是命令和Commandrouter实例正在注入使用DaggerCommandRouterFactory,但是无论我做什么,都正在使用commandrouter的无参数构造函数 - 如果我遗漏了编译器,则不满意,所以我有两个构造函数,只注入了我需要的构造函数,但它仍在打电话给它没有参数构造函数!

我在线遇到了以下文章( https://youtrack.jetbrains.jetbrains.jjjetbrains.com/issue/idea--eisue/idea-------- 190622 ),这表明Intellij正在缓存以前的构建方面,我想这是注射No-参数构造函数的注释,但我对如何纠正它有些困扰。

import java.util.List;

public interface Command {
    String key();
    Status handleInput(List<String> input);

    enum Status {
        INVALID,
        HANDLED
    }
}
import javax.inject.Inject;
import java.util.List;

public final class HelloWorldCommand implements Command {
    @Inject
    HelloWorldCommand(){};

    @Override
    public String key() {
        return "hello";
    }

    @Override
    public Status handleInput(List<String> input) {
        System.out.println(input.size());
        if (!input.isEmpty())
            return Status.INVALID;
        System.out.println("world!");
        return Status.HANDLED;
    }
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ATM.Command.Status;
import javax.inject.Inject;

public class CommandRouter {
    private final Map<String, Command> commands = new HashMap<>();

    @Inject
    CommandRouter(HelloWorldCommand helloWorldCommand){
        System.out.println("HelloWorldCommand Constructor");
        commands.put(helloWorldCommand.key(), helloWorldCommand);
    }

    CommandRouter(){
        System.out.println("No Parameter Constructor");
    }

    Status route(String input) {
        List<String> splitInput = split(input);
        if (splitInput.isEmpty()) {
            return invalidCommand(input);
        }

        String commandKey = splitInput.get(0);
        Command command = commands.get(commandKey);

        if (command == null) {
            return invalidCommand(input);
        }

        Status status = command.handleInput(splitInput.subList(1, splitInput.size()));

        if (status == Status.INVALID){
            System.out.println(commandKey + ": invalid arguments");
        }

        return status;
    }

    private Status invalidCommand(String input) {
        System.out.println(String.format("couldn't understand \"%s\". please try again.", input));
        return Status.INVALID;
    }

    private List<String> split(String input) {
        List<String> tokens = List.of(input.split(" "));
        return tokens;
    }
}
import dagger.Component;

@Component
public interface CommandRouterFactory {
    CommandRouter router();
}
import java.util.Scanner;

public class CommandLineATM {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        CommandRouterFactory commandRouterFactory = DaggerCommandRouterFactory.create();
        CommandRouter commandRouter = commandRouterFactory.router();

        while(scanner.hasNextLine()){
            commandRouter.route(scanner.nextLine());
        }
    }
}

I'm following along with the Dagger ATM tutorial online and have hit issues at the end of the page: https://dagger.dev/tutorial/03-first-command

I've dumped all of the code below for ease of reference but essentially what I understand to be happening is the Command and CommandRouter instances are being injected into the DaggerCommandRouterFactory but no matter what I do the no-parameter constructor for CommandRouter is being used - the compiler isn't happy if I leave it out, so I had two constructors and only injected the one I needed, but it was STILL calling the no-parameter constructor!

I came across the following article online (https://youtrack.jetbrains.com/issue/IDEA-190622), which suggests that IntelliJ is caching aspects of the previous builds and I guess within that is the annotation injecting the no-parameter constructor but I'm a bit stuck on how to rectify it.

import java.util.List;

public interface Command {
    String key();
    Status handleInput(List<String> input);

    enum Status {
        INVALID,
        HANDLED
    }
}
import javax.inject.Inject;
import java.util.List;

public final class HelloWorldCommand implements Command {
    @Inject
    HelloWorldCommand(){};

    @Override
    public String key() {
        return "hello";
    }

    @Override
    public Status handleInput(List<String> input) {
        System.out.println(input.size());
        if (!input.isEmpty())
            return Status.INVALID;
        System.out.println("world!");
        return Status.HANDLED;
    }
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ATM.Command.Status;
import javax.inject.Inject;

public class CommandRouter {
    private final Map<String, Command> commands = new HashMap<>();

    @Inject
    CommandRouter(HelloWorldCommand helloWorldCommand){
        System.out.println("HelloWorldCommand Constructor");
        commands.put(helloWorldCommand.key(), helloWorldCommand);
    }

    CommandRouter(){
        System.out.println("No Parameter Constructor");
    }

    Status route(String input) {
        List<String> splitInput = split(input);
        if (splitInput.isEmpty()) {
            return invalidCommand(input);
        }

        String commandKey = splitInput.get(0);
        Command command = commands.get(commandKey);

        if (command == null) {
            return invalidCommand(input);
        }

        Status status = command.handleInput(splitInput.subList(1, splitInput.size()));

        if (status == Status.INVALID){
            System.out.println(commandKey + ": invalid arguments");
        }

        return status;
    }

    private Status invalidCommand(String input) {
        System.out.println(String.format("couldn't understand \"%s\". please try again.", input));
        return Status.INVALID;
    }

    private List<String> split(String input) {
        List<String> tokens = List.of(input.split(" "));
        return tokens;
    }
}
import dagger.Component;

@Component
public interface CommandRouterFactory {
    CommandRouter router();
}
import java.util.Scanner;

public class CommandLineATM {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        CommandRouterFactory commandRouterFactory = DaggerCommandRouterFactory.create();
        CommandRouter commandRouter = commandRouterFactory.router();

        while(scanner.hasNextLine()){
            commandRouter.route(scanner.nextLine());
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文