java - Spring 4 Component Inject with generic -
i have encountered following issue. create spring @component generic
@component public class responsedtovalidator<dto> { public responseentity<dto> methodtoinvoke(dto dto) { return optional.ofnullable(dto).map(result -> new >responseentity<> (result, httpstatus.ok)) .orelse(new responseentity<>(httpstatus.not_found)); } } @controller public class somecontroller { @inject private responsedtovalidator<dto1> responsedtovalidator1; @inject private responsedtovalidator<dto2> responsedtovalidator2; public void somemethod() { dto1 dto1 = new dto1(); dto2 dto2 = new dto2(); responsedtovalidator1.methodtoinvoke(dto1); responsedtovalidator2.methodtoinvoke(dto2); } }
can inject component above? actually, have tried , seems work properly, can please confirm correct or not?
firstly, spring bean can not injected in itself.
in regards question,yes injectable dont use generic signs when injecting. inject normally. generic type send when method of generic class utilizing.
for instance;
@component public class responsedtovalidator<dto> { public void getabc(list<dto> alist) {} }
then;
public class test { @autowired // or @inject private responsedtovalidator responsedtovalidator; public void testmethod() { responsedtovalidator.getabc(list<enteratypeinhere> alist); } }
Comments
Post a Comment