java - Guice FactoryBuilderModule NullPointerException On Generated Factory -
i new guice , have done lot of reading on have not hand success this. creating dao , want use guice , assistedinjection. end goal create injected factory in other classes throughout application.
intended use in actual class have injection of factory classes it
public class testallmodelbootstrap { @inject private daofactory factory; public testallmodelbootstrap() { } @test public void testgettingdao() { injector injector = guice.createinjector(new hibernatedaomodule()); token otoken = new opertokenv1(); accountingdao accountingdao = factory.create(otoken); } }
this based on guice-based code of:
public interface daofactory { public accountingdao create(token oticket); }
the concrete class has constructor annoated
@inject public hibernateaccountingdao(@assisted token oticket) { this.oticket = oticket; }
and actual module:
@override protected void configure() { install(new factorymodulebuilder() .implement(accountingdao.class, hibernateaccountingdao.class) .build(daofactory.class)); bind(sessionfactoryinterface.class) .toprovider(hibernatesessionprovider.class); }
each time try run this: java.lang.nullpointerexception -> indicating the:
factory.create(otoken);
has factory null. in reading on problem lead believe injection not work using in "test" class. needs put in "injected" class itself. doesn't work either - if wrapper factory injection in class , try use it, doesn't work.
any appreciated...
testallmodelbootstrap
did not come injector—junit created instead—so guice hasn't had chance inject yet. guice can inject objects creates via getinstance
(and objects' dependencies, recursively), or objects passed injectmembers
, or existing instances requested using requestinjection
.
you can manually factory instance:
factory = injector.getinstance(daofactory.class);
or ask guice inject members using injectmembers
:
injector.injectmembers(this); // sets @inject factory field
or use tool guiceberry inject test cases across app.
Comments
Post a Comment