我刚刚实现了像这里描述的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();
...
}
我无法理解为什么他们有时候是空的而有些时候不是。(在同一个班级内)
答案 0 :(得分:13)
@Initbinder
必须声明为public
。