【问题标题】:What is actually involved in a mvp scenario to use google guice?在 mvp 场景中使用 google guice 实际涉及什么?
【发布时间】:2018-02-03 19:50:39
【问题描述】:

我非常熟悉使用@Bean 和@Autowired 进行spring 注入。我已经转而查看 guice,我想知道让它发挥作用的最低要求是什么。以下基本示例引发 NPE:

import javax.inject.Inject;

public class ClassA {

    String a = "test";

    @Inject
    public ClassA() {
        System.out.println(a);
    }

    public void go() {
        System.out.println("two");
    }
}

以下类试图实例化 ClassA 的新实例:

import javax.inject.Inject;

public class ClassB {

    @Inject
    ClassA guice;

    public ClassB() {
        guice.go();
    }

    public static void main(String[] args) {
        ClassB b = new ClassB();
    }

}

我尝试了以下各种组合,但均未成功:

public class SupportModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(ClassA.class);
        //bind(ClassA.class).to(ClassB.class);
        //others too

    }
}

我一定是在这里遗漏了一个关键的东西,我不太确定在哪里?我需要手动实例化 guice/配置本身吗?我想我可能会。

guice.go();

在 Spring 中,我可以执行以下操作,我认为 Guice 也可以帮助我执行此操作:

@Bean
public FrameworkProperties properties() {
    return new FrameworkProperties();
}

然后只是:

@Autowired
FrameworkProperties props;

【问题讨论】:

    标签: java guice


    【解决方案1】:

    我是否需要手动实例化 guice/配置本身?我想我可能会。

    是的,你猜对了。您必须引导您使用 Guice.createInjector() 方法定义的注入器模块。另外,需要注意的另一件事是,在使用 ClassB 中的自定义构造函数时,您必须使用构造函数注入。所以为了让它工作,ClassB 应该是这样的:

    public class ClassB {
    
        private ClassA guice;
    
        @Inject //This will inject the dependencies used in the constructor arguments
        public ClassB(final ClassA guice) {
            this.guice = guice;
            guice.go();
        }
    
        public static void main(String[] args) {
            /**
             * If only type to type binding is required, you can skip creating a Module class &
             * bootstrap the injector with empty argument createInjector like used below. But, when
             * there are other kind of bindings like Type to Implementations defined in modules, you can use:
             * final Injector injector1 = Guice.createInjector(new GuiceModule1(), new GuiceModule2());
             */
            final Injector injector = Guice.createInjector();
            final ClassB b = injector.getInstance(ClassB.class); //This will create dependency graph for you and inject all dependencies used by ClassB and downwards
        }
    }
    

    此外,您可以删除 ClassA 的构造函数中使用的 @Inject 注释,因为您没有在该构造函数中注入任何外部依赖项。您可以查看 Guice 的入门 wiki 以获取更多文档。

    【讨论】:

    • 说我想要一个简单的对象 A 作为对象 B 中的实例变量,那里需要什么?我需要继续引用和输入注射器代码吗?如果我的应用程序没有 main 方法怎么办?
    • 如果没有在构造函数中使用,则使用字段注入。这将与 spring 相同,只需将 @Autowired 替换为 @Inject
    • 但我需要在某个地方设置喷油器等?我的自动化框架中没有 main 方法,并且我不想在每个使用它的类中键入注入器代码?
    • 您只需引导一次。如果没有 main 方法,那么测试中的配置方法,例如 @BeforeSuite@BeforeClass,它们将在测试执行开始时运行一次。
    • 我将使用我的侦听器尝试一次引导程序,该侦听器在测试执行周期开始时运行,现在对此进行调查:)
    猜你喜欢
    • 2016-01-07
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多