bineanzhou / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

bind(SomeConcreteType.class).toConstructor() #171

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Right now bind(Impl.class).to(Impl.class) gives an error saying "Binding
points to itself", but that's actually allowed by just calling
bind(Impl.class). bind(Impl.class).to(Impl.class) should just work as expected.

Original issue reported on code.google.com by wyue...@gmail.com on 18 Dec 2007 at 6:42

GoogleCodeExporter commented 9 years ago
See thread

[http://groups.google.com/group/google-guice/browse_thread/thread/5845897c2f5eee
08]

Original comment by wyue...@gmail.com on 18 Dec 2007 at 6:45

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 14 May 2008 at 4:27

GoogleCodeExporter commented 9 years ago
I changed the issue description from  "bind(Impl.class).to(Impl.class) should 
work"
to "bind(SomeConcreteType.class).toConstructor() That's how I believe we should 
solve
this use case.

Binding a type to itself is misleading, since it suggests that
  bind(Foo.class).to(RealFoo.class)
means 'bind Foo to the constructor of RealFoo', whereas it really means 'bind 
Foo to
the binding for RealFoo'.

See also issue 231. 

Original comment by limpbizkit on 31 Oct 2008 at 8:31

GoogleCodeExporter commented 9 years ago
Implemented over a series of changes:
  r1005 adds SPI access
  r1022 implements it
  r1023, r1024 r1025: tests, tests, tweaks
We may consider implementing "@New" on top of toConstructor() bindings.

One interesting consequence of toConstructor is the impact on scoping. Suppose 
you bind the same type 
under two different keys:
  @Singleton
  class A {}

  public class BindSomeAsModule extends AbstractModule {
    public void configure() {
      bind(A.class).annotatedWith(named("1")).toConstructor(A.class.getDeclaredConstructor());
      bind(A.class).annotatedWith(named("2")).toConstructor(A.class.getDeclaredConstructor());
    }
  }
Ultimately, you'll get two instances of A, one for each binding. It seems 
awkward until you consider the fact 
that the two bindings could be pointing at distinct constructors, in which case 
it would be impossible to use a 
single instance.

Original comment by limpbizkit on 21 Jun 2009 at 6:20

GoogleCodeExporter commented 9 years ago
I think that is acceptable. having two bind rules that chain toCtor implies 
wanting two 
separate instances methinks. The ctor is the end point of every bind rule 
anyway 
(whether implicitly or in this awesome new jessefeature)

Original comment by dha...@gmail.com on 21 Jun 2009 at 9:46