5

Я просто реализован AspectJ, как описано здесь: https://stackoverflow.com/a/10998044/2182503AspectJ: Autowired поля равны нулю в Initbinder

Это решение работает хорошо, пока я не заметил, что мои @Autowired поля равны нулю в @InitBinder. Поля имеют только нулевое значение в пределах @InitBinder.

@Controller 
public class EmployeeController { 
    @Autowired private GenericDaoImpl<Role, Integer> roleDao; 
    @Autowired private GenericDaoImpl<Employee, Integer> employeeDao; 
    @Autowired private EmployeeValidator employeeValidator; 

    @InitBinder 
    private void initBinder(WebDataBinder binder) { 
     // autowired fields are null 
     binder.setValidator(employeeValidator); 
     binder.registerCustomEditor(Set.class, "roles", new CustomCollectionEditor(Set.class) { 
      protected Object convertElement(Object element) { 
       if (element != null) { 
        Integer id = new Integer((String) element); 
        Role role = roleDao.findById(id); 
        return role; 
       } 
       return null; 
      } 
     }); 
    } 

    @PreAuthorize("hasRole('MASTERDATA_VIEW')") 
    @RequestMapping(value = { "/employees" }, method = RequestMethod.GET) 
    public ModelAndView showEmployeeList() { 
     // dao not null 
     List<Employee> employees = employeeDao.findAll(); 
      ... 
    } 

Я не могу понять, почему они иногда утратившим не somethimes. (В пределах того же класса)

ответ

10

@Initbinder должен быть объявлен как public.

+0

Спасибо, один из методов моего контроллера был отмечен как закрытый. Не могли понять, почему все остальные работают, а другого нет. – Hiro2k

Смежные вопросы