java - @Autowired in Spring custom converter -
this question has answer here:
- why spring @autowired field null? 10 answers
i have custom converter:
@component public class roleconverter implements converter<string, role> { @autowired private roles roles; @override public role convert(string id) { return roles.findbyid(long.parselong(id)); } }
but @autowired setting null value. causing nullpointerexception
.
this roles class:
@repository @transactional public class roles extends domain<role>{ public roles() { super(role.class); } }
i'm using java configuration. converter registered:
@configuration @enablewebmvc //other annotations... public class webappconfig extends webmvcconfigureradapter { //.... @override public void addformatters(formatterregistry registry) { registry.addconverter(new roleconverter()); super.addformatters(registry); } /.... }
when @autowired roles in controller works.
why @autowired setting null in converter?
its because here creating new object of roleconverter
. instead should autowire roleconverter
registry.addconverter(new roleconverter());
Comments
Post a Comment