How to do bootstrap injection using derived class

67 views
Skip to first unread message

Farrukh Najmi

unread,
Apr 15, 2016, 8:56:14 AM4/15/16
to Dagger Discuss
I am using Dagger 1 in a Java Swing UI project. It has a main class named "com.foo.Main". It has a Dagger Module class like this:

@Module(
    injects
= {
       
Main.class,
       
Other.class
   
},

    complete
= false,
    library
= true
)
public class SomeModule {
   
@Provides
   
Other provideOther() {
       
...
   
}
}


The Main class bootstraps the injection like this:

public class Main {
    public Main()
    {
        ...
        initializeObjectGraph();
    }

    public static void main(final String[] args)
    {
        Main main = new Main();
        ...
    }

    private void initializeObjectGraph() {
    try {
            ObjectGraph objectGraph = ObjectGraph.create(new SomeModule());
            objectGraph.inject((Main)this);
        } catch (Exception e) {
           ...
        }      
    } 
}

I now create a derived class of Main "com.foo.Derived" as follows:

public class Derived extends Main {
    public Derived ()
    {
        ...
    }

    public static void main(final String[] args)
    {
        Derived derived = new Derived();
        ...
    }
}

When I run Derived class I get the following stacktrace:

java.lang.IllegalArgumentException: No inject registered for members/com.foo.Derived. You must explicitly add it to the 'injects' option in one of your modules.
 at dagger
.ObjectGraph$DaggerObjectGraph.getInjectableTypeBinding(ObjectGraph.java:302)
 at dagger
.ObjectGraph$DaggerObjectGraph.inject(ObjectGraph.java:279)
 at com
.foo.Main.initializeObjectGraph(Main.java:313)

It appears that dependency graph is build at compile time not run time.

What do folks advice in this situation? Is there a good workaround? Does Dagger 2 address this problem any better? TIA.

Reply all
Reply to author
Forward
0 new messages