Guice Inject Field in class not created by Guice

Viewed 10871

I have a class like so, that I create myself somewhere in my code:

class StarryEyes {
   @Inject MyValidator validator;

   public StarryEyes(String name) {
      //..
   }

   public doSomething() {
      // validator is NULL
   }
}

I want Guice to inject an instance of validator, which has a @Singleton annotation. I have a module that's loaded at startup and it contains the line:

bind(MyValidator.class);

However, it doesn't seem to work as "validator" is always null. I've tried a number of variations like:

bind(MyValidator.class)toInstance(new MyValidator());

or other things like that. Is this not how Guice is supposed to work?

1 Answers
Related