Question

I just implemented AspectJ like described here: https://stackoverflow.com/a/10998044/2182503

This solution works fine, until I noticed that my @Autowired fields are null within @InitBinder. The fields are only null within the @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();
            ...
    }

I cannot comprehend why they are sometimes null and somethimes not.(within the same class)

Was it helpful?

Solution

@Initbinder must be declared as public.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top