java - @Autowired in Spring custom converter -


this question has answer here:

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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -