AspectJ:Initbinder中的自动连接字段为空

时间:2014-02-07 11:28:41

标签: spring spring-mvc aspectj aspectj-maven-plugin spring-mvc-initbinders

我刚刚实现了像这里描述的AspectJ:https://stackoverflow.com/a/10998044/2182503

此解决方案正常,直到我注意到@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();
            ...
    }

我无法理解为什么他们有时候是空的而有些时候不是。(在同一个班级内)

1 个答案:

答案 0 :(得分:13)

@Initbinder必须声明为public