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
Post a Comment