Guice Beginner - 如何绑定具体类?

时间:2022-09-25 10:54:40

I have this class:

我有这堂课:

public class House {
    private final Door door;
    private final Window window;
    private final Roof roof;

    @Inject
    public House(Door door, Window window, Roof roof) {
        this.door = door;
        this.window = window;
        this.roof = roof;
    }
}

Where Door, Window and Roof are concrete classes. Now if I want to implement a Module for this scenario, I would do it like this:

门,窗和屋顶是混凝土类。现在,如果我想为这个场景实现一个模块,我会这样做:

public class HouseModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(Door.class).to(Door.class);
        bind(Window.class).to(Window.class);
        bind(Roof.class).to(Roof.class);
    }
}

But I wonder if this is the right way to bind concrete classes, or if there are easier ways. I feel there is an easier way to this.

但我想知道这是否是绑定具体类的正确方法,或者是否有更简单的方法。我觉得有一个更简单的方法。

EDIT

Just tried this out, and it doesn't seem to work:

刚试了这个,它似乎不起作用:

1) Binding points to itself.
  at de.tarent.guice.ex._1.HouseModule.configure(HouseModule.java:10)

EDIT 2

It seems like no binding is needed at all:

似乎根本不需要绑定:

Injector injector = Guice.createInjector();
House house = injector.getInstance(House.class);

Also seems to work.

似乎也有效。

3 个解决方案

#1


24  

Guice's Just-In-Time binding does exactly what you want. Given your Door, Window and Roof meet following requirements (quoted from the Guice documentation):

Guice的Just-In-Time绑定完全符合您的要求。鉴于您的门,窗和屋顶符合以下要求(引自Guice文档):

either a public, no-arguments constructor, or a constructor with the @Inject annotation

公共,无参数构造函数或带有@Inject批注的构造函数

an empty Module implementation will be sufficient:

一个空的模块实现就足够了:

public class HouseModule extends AbstractModule {
    @Override
    protected void configure() {
    }
}

#2


42  

This is the way to go:

这是要走的路:

protected void configure() {
    bind(Door.class);
    bind(Window.class);
    bind(Roof.class);
}

Since they are concrete classes, as Guice says, you can't bind them to themselves :-)

由于它们是具体的类,正如Guice所说,你不能将它们绑定到自己:-)

Check out the Binder docs, it notes:

查看Binder文档,它注意到:

bind(ServiceImpl.class);

This statement does essentially nothing; it "binds the ServiceImpl class to itself" and does not change Guice's default behavior. You may still want to use this if you prefer your Module class to serve as an explicit manifest for the services it provides. Also, in rare cases, Guice may be unable to validate a binding at injector creation time unless it is given explicitly.

这句话基本上没有任何意义它“将ServiceImpl类绑定到自身”并且不会改变Guice的默认行为。如果您更喜欢Module类作为其提供的服务的显式清单,您可能仍希望使用此方法。此外,在极少数情况下,Guice可能无法在注射器创建时验证绑定,除非明确给出。

Concrete classes with constructor marked as @Inject are automatically available for injection. But it helps the developer (you) know what is configured in the module.

带有标记为@Inject的构造函数的具体类可自动注入。但它有助于开发人员(您)知道模块中配置的内容。

#3


6  

Binding is needed to link Interface and Implementation class (to change to other implementation in test env for example). But since you have concrete classes, no need for binding to, just bind classes

链接Interface和Implementation类需要绑定(例如,更改为测试环境中的其他实现)。但由于你有具体的类,不需要绑定,只需绑定类

#1


24  

Guice's Just-In-Time binding does exactly what you want. Given your Door, Window and Roof meet following requirements (quoted from the Guice documentation):

Guice的Just-In-Time绑定完全符合您的要求。鉴于您的门,窗和屋顶符合以下要求(引自Guice文档):

either a public, no-arguments constructor, or a constructor with the @Inject annotation

公共,无参数构造函数或带有@Inject批注的构造函数

an empty Module implementation will be sufficient:

一个空的模块实现就足够了:

public class HouseModule extends AbstractModule {
    @Override
    protected void configure() {
    }
}

#2


42  

This is the way to go:

这是要走的路:

protected void configure() {
    bind(Door.class);
    bind(Window.class);
    bind(Roof.class);
}

Since they are concrete classes, as Guice says, you can't bind them to themselves :-)

由于它们是具体的类,正如Guice所说,你不能将它们绑定到自己:-)

Check out the Binder docs, it notes:

查看Binder文档,它注意到:

bind(ServiceImpl.class);

This statement does essentially nothing; it "binds the ServiceImpl class to itself" and does not change Guice's default behavior. You may still want to use this if you prefer your Module class to serve as an explicit manifest for the services it provides. Also, in rare cases, Guice may be unable to validate a binding at injector creation time unless it is given explicitly.

这句话基本上没有任何意义它“将ServiceImpl类绑定到自身”并且不会改变Guice的默认行为。如果您更喜欢Module类作为其提供的服务的显式清单,您可能仍希望使用此方法。此外,在极少数情况下,Guice可能无法在注射器创建时验证绑定,除非明确给出。

Concrete classes with constructor marked as @Inject are automatically available for injection. But it helps the developer (you) know what is configured in the module.

带有标记为@Inject的构造函数的具体类可自动注入。但它有助于开发人员(您)知道模块中配置的内容。

#3


6  

Binding is needed to link Interface and Implementation class (to change to other implementation in test env for example). But since you have concrete classes, no need for binding to, just bind classes

链接Interface和Implementation类需要绑定(例如,更改为测试环境中的其他实现)。但由于你有具体的类,不需要绑定,只需绑定类