java - Guice Inject Field in class not created by Guice -


I have a class that I will make somewhere in my code:

  Class StarryEyes {@Intject MyValidator validator; Public StarryEyes (string name) {//}} public doSomething () {// validator NULL}}  

I would like to inject a frequency of the validator, in which a signature annotation . I have a module loaded on startup and has a line in it:

  bound (MyValidator.class);  

However, this does not seem to work as "legalization" is always zero. I have tried many variations such as:

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

Or other things like that. Is not it that Guice should work?

Generally Guice needs to create an object to inject them. If you simply call new starry (name) , the device will never be able to see that item, so that it will not be able to inject it. The one thing you can do is that you can call injector.injectMembers (obj) on the object when you create it. I do not recommend that, however, as you should avoid referring to the injector in your code.

What you really want here with auxiliary injection, you can declare the constructor for your class in this way:

  @ Public Starli Eines (@ Injecting the MySQLidator verifier, @assized string name)  

This means that validator is a parameter that must be injected into the GUI, while The name should be "Assisted" (that is, for example, B Has been taken).

Then you create an interface like this:

  Public Interface StarryEyesFactory {StarryEyes create (string name); }  

With assisted injection, Guice can apply that factory to you, you can tie it like this:

  Tie Efficiency class.) Provider (Factory Provider. NFFactinator (StarlyExfactor Class, Starry Aces Class));  

You can again inject a StarryEyesFactory , anywhere you want to create an example of where you new StarryEyes (name) could say earlier, now you call starryEyesFactory.create (name) . When you call on the phone (name) , it will take the name and pass it to the constructor and will provide itself the bound validator.

Start at Guice 3 , you do this:

  Install (new factory modulebuilder.) Build (starlinesfactiner.class));  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -