java - Inject not working for nested objects[Jersey 2.22.1] -


i have jersey resource facade object injected. configured in resourceconfig , facade gets injected fine. facade contains dao class should injected , configured in same resourceconfig. problem; dao class null. thus, not injected.

@applicationpath("/service") public class systemsetup extends resourceconfig {  public systemsetup() {     packages(false, "com.foo.bar");     packages("org.glassfish.jersey.jackson");     register(jacksonfeature.class);      final lockmanager manager = getlockmanager();     final sessionfactory sessionfactory = getsessionfactory();     register(new abstractbinder() {         @override         protected void configure() {             bindfactory(injectfactory.getdaofactory(sessionfactory)).to(dao.class).in(singleton.class);             bindfactory(injectfactory.getfacadefactory(manager)).to(facade.class).in(singleton.class);         }     }); } 

 @path("/")  @produces("text/json")  public class viewresource {      @inject     private facade logic; 

public class facade {      @inject     private dao dao; //not injected 

the factory instances rather simple. call constructor , pass argument it.

the strange thing worked absolut fine when used bind(class object) rather bindfactory.

edit

factories

class injectfactory {      static factory<dao> getdaofactory() {         return new factory<dao>() {             @override             public dao provide() {                 return new dao(new object());             }              @override             public void dispose(dao dao) {}         };     }      static factory<facade> getfacadefactory() {         return new factory<facade>() {              @override             public facade provide() {                 return new facade();             }              @override             public void dispose(facade facade) {}         };     } } 

as case di frameworks, when start instantiating things yourself, it's case kicking framework out of equation. holds true factory instances, objects factory creates. facade instance never gets touch framework, except inject resource class.

you can can hold of servicelocator, , explicitly inject objects if want create them yourself. here couple options.

1) inject servicelocator factory instance, inject facade instance.

static factory<facade> getfacadefactory() {     return new factory<facade>() {          @context         servicelocator locator;          @override         public facade provide() {             facade facade = new facade();             locator.inject(facade);             return facade;         }          @override         public void dispose(facade facade) {}     }; }  @inject public systemsetup(servicelocator locator) {     packages("foo.bar.rest");     packages("org.glassfish.jersey.jackson");     register(jacksonfeature.class);      register(new abstractbinder() {         @override         protected void configure() {             bindfactory(injectfactory.getdaofactory()).to(dao.class);              factory<facade> factory = injectfactory.getfacadefactory();             locator.inject(factory);             bindfactory(factory).to(facade.class);         }     }); } 

2) or bind factory class, , let framework inject servicelocator

public static class facadefactory implements factory<facade> {      @context     servicelocator locator;      @override     public facade provide() {         facade facade = new facade();         locator.inject(facade);         return facade;     }      @override     public void dispose(facade facade) {} }  register(new abstractbinder() {     @override     protected void configure() {         bindfactory(injectfactory.getdaofactory()).to(dao.class);         bindfactory(injectfactory.facadefactory.class).to(facade.class);     } }); 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -