java - Autowiring inside abstract class made for mapstruct -


i trying build rest controller using spring. format data readability , more integration, have used mapstruct. here's how wrote mapper.

@mapper public abstract class devicedatamapper {  @autowired deviceservice deviceservice;  public static devicedatamapper instance = mappers.getmapper(devicedatamapper.class);  @mappings({     @mapping(source = "deviceid", target = "iddevice"),     @mapping(source = "devicename", target = "name") }) public abstract todevice devicetotodevice(devicedata device);  public devicedata todevicetodevicedata(todevice todevice){     devicedata devicedata = new devicedata();     devicedata.setdeviceid(todevice.getiddevice());     devicedata.setdevicename(todevice.getname());     devicedata.setdevicetemplateid(todevice.getdevicetemplateid());     try { devicedata.setdevicetemplatename(deviceservice.finddevicetemplatebyid(todevice.getdevicetemplateid()).getname());     } catch (exception e) {         e.printstacktrace();     }      return devicedata; }} 

the api controller function looks this

@requestmapping(value = "/{deviceid}",method = requestmethod.get) public @responsebody devicedata get(@pathvariable int deviceid) {     devicedata devicedata=new devicedata();     try {         devicedata =    devicedatamapper.instance.todevicetodevice(deviceservice.findone(deviceid));     } catch (exception e) {         e.printstacktrace();     }     return devicedata; } 

the output devicedata returns fine except 1 detail. couldn't function deviceservice.finddevicetemplatebyid(todevice.getdevicetemplateid() (where deviceservice autowired). error stack trace shows me nullpointerexception. wondering whether there general rule accessibility of autowired resources in abstract class? or way instantiating makes function inaccessible? should change make work? have tried @inject javax.inject same result.

you use spring component model mapper:

@mapper(componentmodel="spring") public abstract class devicedatamapper {     ... } 

that way can inject dependencies (e.g. other hand-written uses) inject mapper other classes instead of resorting instance pattern.


Comments