Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspectJ: Autowired fields are null in Initbinder

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)

like image 972
Tunguska Avatar asked Feb 07 '14 11:02

Tunguska


1 Answers

@Initbinder must be declared as public.

like image 141
Tunguska Avatar answered Sep 18 '22 23:09

Tunguska