java - bootstrapping guice injector -


i have scenario need objects injecting @ start logging, other objects injected on demand. don't know if in console application, call

guice.createinjector(....) 

in main function , when need object, type need on demand, use provider inject same object again? i'm having hard time figuring out best way use guice's injector scenario. in advance.

in general, yes: inject provider objects later, , don't hold on injector directly longer necessary.

ideally, bootstrap should that: should instantiate injector , sort of whole-application instance. particularly important because guice helpful testing, maximizing part of application subject guice idea.

public class yourapplication {    public static void main(string[] args) {     // 3 lines exist outside object graph.     injector injector = guice.createinjector(new yourmodule1(), new yourmodule2());     yourapplication app = injector.getinstance(yourapplication.class);     app.run();   }    @inject provider<yourdep1> depprovider1;   @inject yourdep2 dep2;    public void run() {     // here have access every @inject field.   } } 

here, bear in mind injector created kept on stack. because need @inject-annotated fields, methods, , constructors, including injector itself. suggested, can use provider many instances need, including 0 instances if object isn't necessary in code path.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -