ステートメント " " を作りましょうAll Rocks have Minerals.:
public class Mineral
{
// Nevermind why a Mineral would have a GUID.
// This is just to show that each Mineral instance
// is universally-unique.
String guid;
@Inject
public Mineral(String id)
{
guid = id;
}
}
public class Rock
{
private Mineral mineral;
@Inject
public Rock(Mineral min)
{
mineral = min;
}
}
2 つRockのインスタンスが必要な場合、それぞれが異なるMineralインスタンス (それぞれ独自の GUID を持つ) で構成されています。
public class RockModule extends AbstractModule
{
public void configure(Binder binder)
{
// Make two Minerals with different GUIDs.
Mineral M1 = new Mineral(UUID.getRandomId().toString());
Mineral M2 = new Mineral(UUID.getRandomId().toString());
// Configure two Rocks with these unique Minerals
Rock R1 = new Rock(M1);
Rock R2 = new Rock(M2);
// Define bindings
bind(Rock.class).toInstance(R1);
// No way for Guice to expose R2 to the outside world!
}
}
そのため、Guice に を要求すると、それ自体が のインスタンスで構成されているインスタンスRockが常に返されます。R1M1Mineral
Spring DI では、2 つの Bean を同じタイプに定義できますが、異なる Bean ID を与えるだけです。次に、ID を使用して Bean を「接続」します。したがって、 andと一緒に、R1および一緒に、などを配線することができます。その後、必要に応じて、または必要に応じてSpring に要求できます。M1R1M2R1R2
Guice では、インスタンスではなく、必要な型( Rock.class)のみを要求できます。
Guice でさまざまな「有線 Bean」をどのように要求しますか? 異なるAbstractModuleコンクリートを使用することによって?それとも、これは Guice の制限ですか?