java - Simple Dependency injection not working -


it's first time have use dependency injection , i'm little confused.

i don't understand how works.

i have tried on simple example :

public class stockresponse extends response {     @inject brandservice $brand;      public list<stockresponseitem> stock;      public stockthresholdresponse()     {         stock = new arraylist<>();     }      public static stockthresholdresponse create(list<dataitem> data)     {         stockresponse stock= new stockresponse();          (thresholdcheckaggregate data: d)         {             stockresponseitem item = new stockresponseitem();              item.id = d.thresholdid;             item.brand = str.$brand.byid(d.brand);              str.stockthresholds.add(item);         }         return str;     } } 

but when use create() method, null pointer exception $brand.

i think have misunderstood how di works can't find error.

i had similar difficulties understand how di (guice out of java ee) works. in simple words guice must have chance modify object, example:

  • assist construction usually. ask guice "can create object" injector.getinstance(cls), guice creating object you, solving field or constructor annotation

in normal (non java ee) environment yoy never call classic constructor, ask second hand.

  • other method. few library / frameworks have integration guice (apache wicket like) "creation listeners" on types of objects. hard work of di hidden eyes, executed.

  • java ee lets better ee programmers me :(

in consequence yoy don't give chance inject anything, null

professionals sorry @ blondie level. way discovered di few years ago

correction code. not

stockresponse stock= new stockresponse(); 

but

 mod = .... // module     injector = guice.createinjector(mod); // global or global  ...    injector.getinstance(stockresponse.class); 

edit: intentionally don't answer "how write guice module", assume other, long story


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -