java - Autowiring a generic component in a generic superclass -
i have following situation in spring 4.0 (using spring boot) environment:
mapping interface:
public interface entitymodelmapper<entity extends abstractentity, model extends abstractmodel>{ }
mapping implementation:
@component public class productentitymodelmapper implements entitymodelmapper<product, productmodel>{ }
service:
public interface crudservice<model extends abstractmodel>{ }
and want abstract superclass service this:
public abstract abstractcrudservice<entity extends abstractentity, model extends abstractmodel> implements crudservice<model>{ @autowired private entitymodelmapper<entity, model> mapper; public entitymodelmapper<entity, model> getmapper(){ return mapper; } }
so can have implementations likes this:
@service public productcrudservice extends abstractcrudservice<product, productmodel>{ public void somemethod(product product){ productmodel model = getmapper().map(product); } }
but spring tells me can't find qualifying beans of entitymodelmapper inject in service classes. scenario possible , i'm doing wrong or pushing limits of spring's dependecy injection?
stacktrace:
org.springframework.beans.factory.beancreationexception: error creating bean name 'productcrudservice': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private com.flycatcher.seagull.mapper.entitymodelmapper com.flycatcher.seagull.facade.service.crud.abstractcrudservice.mapper; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.flycatcher.seagull.mapper.entitymodelmapper] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}
according this: https://spring.io/blog/2013/12/03/spring-framework-4-0-and-java-generics possible since spring 4.0 version.
that didn't work me either. (same exception , im using 4.2.3). try upgrade latest version - 4.2.6.
if still doesn't work, can instead use @qualifier annotation , autowire entitymodelmapper interface in sub-class, , define getmapper abstract:
@component @qualifier("productentitymodelqualifier") public class productentitymodelmapper implements entitymodelmapper<product, productmodel>{ }
and in productcrudservice:
@service public productcrudservice extends abstractcrudservice<product, productmodel>{ @autowired @qualifier("productentitymodelqualifier") entitymodelmapper<product, productmodel> mapper; @override protected entitymodelmapper<product, productmodel> getmapper(){return mapper;} public void somemethod(product product){ productmodel model = getmapper().map(product); } }
Comments
Post a Comment