google / guice

Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 11 and above, brought to you by Google.
https://github.com/google/guice
Apache License 2.0
12.45k stars 1.66k forks source link

Do not fail on circular constructor/setter dependency #690

Open gissuebot opened 10 years ago

gissuebot commented 10 years ago

From stepan.koltsov on March 07, 2012 14:42:18

public static class A {     private final B b;

    @Inject     public A(B b) {         this.b = b;     } }

public static class B {     private A a;

    @Inject     public void setA(A a) {         this.a = a;     } }

public static void main(String[] args) {     Guice.createInjector(new AbstractModule() {         @Override         protected void configure() { }     }).getInstance(A.class);

}

guice reports error:

Tried proxying com.company.Temp$A to support a circular dependency, but it is not an interface

Proxy is not needed here: guice should just create "B" first, inject it into "A" constructor and then inject "A" into "B".

Original issue: http://code.google.com/p/google-guice/issues/detail?id=690

marcosdeveloper commented 7 years ago

I got this message for another link too: https://github.com/google/guice/issues/139. First of all I think that the @Inject doesn't refer to concrete classes and yes, interfaces only. I guess that the message have to improve to:

Tried proxying projectGuiceTest.A to support a circular dependency, but it is not an interface. You have to break the circular dependency, or make the circular dependency refers to interfaces.

In this way I think that we clarify more the user who will make the best decision.

I will made the changes to improve it.